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


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

      

    待人以诚,做事用心,对事不对人.
  • 相关阅读:
    [bzoj1054][HAOI2008]移动玩具
    atal error C1083: 无法打开包括文件:“stdint.h”
    诛仙手游宝石和灌注性价比分析
    诛仙手游攻略
    商务沟通方法与技巧
    韩服LOL符文翻译
    如何关闭"QQ网购每日精选"信息提醒
    LOL 韩服下载地址
    [经验分享] YY客服联系方式
    广州打印社保明细-网上打印-社保局地址
  • 原文地址:https://www.cnblogs.com/jiangguang/p/2880863.html
Copyright © 2020-2023  润新知