zoukankan      html  css  js  c++  java
  • 进程调用等待及进程退出监控源码

     public partial class FormMain : Form
        {
            public FormMain()
            {
                InitializeComponent();
            }
            /// <summary>
            /// 开启一个程序并等待程序关闭
            /// </summary>
            /// <param name="appPath">程序绝对路径</param>
            /// <param name="args">参数</param>
            public void RunAppAndWait(string appPath, string args)
            {
    
                System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
    
                //设置外部程序名  
                Info.FileName = appPath;
    
                Info.Arguments = args;
                //声明一个程序类  
                System.Diagnostics.Process Proc;
    
                try
                {
                    this.Hide();
                    Proc = System.Diagnostics.Process.Start(Info);
                    Proc.WaitForExit();
                }
                catch (System.ComponentModel.Win32Exception)
                {
                    this.Show();
                    return;
                }
                this.Show();
            }
    
    
            private void btnRunProcess_Click(object sender, EventArgs e)
            {
                RunAppAndWait(txtApp.Text, txtArgs.Text);
            }
    
            private void FormMain_Load(object sender, EventArgs e)
            {
                txtApp.Text = Application.StartupPath + "\\app.exe";
                txtArgs.Text = " abc";
    
            }
    
            private void btnStart_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcessesByName(txtProcess.Text);
                if (procs.Length > 0)
                {
                    foreach (System.Diagnostics.Process item in procs)
                    {
                        item.EnableRaisingEvents = true;
                        item.Exited += new EventHandler(item_Exited);
                    }
                }
    
            }
            public delegate void MyInvoke(string str);
    
            private void SetText(string s)
            {
                if (txtStatus.InvokeRequired)
                {
                    MyInvoke _myInvoke = new MyInvoke(SetText);
                    this.Invoke(_myInvoke, new object[] { s });
                }
                else
                {
                    this.txtStatus.AppendText(s);
                }
            }
    
            void item_Exited(object sender, EventArgs e)
            {
    
                var p = sender as System.Diagnostics.Process;
                SetText(p.ProcessName + "已经退出");
            }
    
        }
    

      

    待人以诚,做事用心,对事不对人.
  • 相关阅读:
    创建大顶堆
    大顶堆的实现
    二叉树的前序,中序,后序,层序实现
    链表实现多项式相加
    225. Implement Stack using Queues
    232. Implement Queue using Stacks
    JavaScript 实现队列操作
    Vue 路由守卫
    回文数 & {}[]() 符号判断

  • 原文地址:https://www.cnblogs.com/jiangguang/p/2880863.html
Copyright © 2011-2022 走看看