프로세스 호출 대기 및 프로세스 종료 모니터링 소스

2558 단어 원본 코드
 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 + "    ");

        }



    }


좋은 웹페이지 즐겨찾기