• 进程调用等待及进程退出监控源码


     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 + "已经退出");
            }
    
        }
    

      

    待人以诚,做事用心,对事不对人.
  • 相关阅读:
    Git failed with a fatal error. Authentication failed
    HttpClient 获取json,即使返回状态不成功也返回json
    win10恢复Windows Media Player
    .NET Core部署到linux(CentOS)最全解决方案
    EasyNetQ 相关问题和解决方法
    RabbitMQ And EasyNetQ
    打爆你的 CPU
    通过代码实现 OutOfMemory
    如何写一段死锁代码
    docker 容器(container)使用ssh服务登录一段时间无操作后自动断开问题解决
  • 原文地址:https://www.cnblogs.com/jiangguang/p/2880863.html
Copyright © 2020-2023  润新知