C# 进程Process基本的操作说明
生活随笔
收集整理的這篇文章主要介紹了
C# 进程Process基本的操作说明
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public int CallPhoneExe(string arg) //arg為進程的命令行參數{
WaitHandle[] waits =new WaitHandle[2]; //定義兩個WaitHandle值,用以控制進程的執行過程waits[0] = HSTOP; //AutoResetEvent HSTOP = new AutoResetEvent(false);waits[1] = GlobalStop;//AutoResetEvent GlobalStop = new AutoResetEvent(false);int iReturn=0; Process p = new Process();//新建一個進程p.StartInfo.Arguments = arg; //進程的命令行參數p.StartInfo.FileName = filepath;//進程啟動路徑p.StartInfo.CreateNoWindow = true;//不顯示新進程的窗口 p.StartInfo.RedirectStandardOutput = true;//輸出重定向p.StartInfo.RedirectStandardError = true; //Redirect the error ouput!p.StartInfo.UseShellExecute = false; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;p.EnableRaisingEvents = true; p.Exited += new EventHandler(p_Exited); //進程自然結束后啟動p—Exited事件p.OutputDataReceived += new DataReceivedEventHandler(ChangeOutput);//進程有輸出時,啟動ChangeOutPut函數 p.Start();//進程啟動p.BeginOutputReadLine();int hstop = WaitHandle.WaitAny(waits);//啟動線程暫停,知道WaitHandle中傳來有效信號switch (hstop)//判斷信號是又哪個{case 0: //進程自然結束if (p.WaitForExit(2000))iReturn = p.ExitCode; //獲取進程的返回值else{CloseProcess();iReturn = -2;}break;case 1: //進程被迫結束p.Kill();//殺掉進程if (!p.HasExited){ p.Kill();} iReturn = -3;break;}HSTOP.Reset(); //HSTOP復位,這個變量指示進程自然結束,每次結束后都得自然復位 p.Close(); //創建的p關閉return iReturn; }private void p_Exited(object sender, EventArgs e){HSTOP.Set();}//輸出重定向函數private void ChangeOutput(object sendingProcess, DataReceivedEventArgs outLine){if (!String.IsNullOrEmpty(outLine.Data)) //字符串不為空時MainForm.FireWriteText(outLine.Data,false);//將進程的輸出信息轉移}
WaitHandle[] waits =new WaitHandle[2]; //定義兩個WaitHandle值,用以控制進程的執行過程waits[0] = HSTOP; //AutoResetEvent HSTOP = new AutoResetEvent(false);waits[1] = GlobalStop;//AutoResetEvent GlobalStop = new AutoResetEvent(false);int iReturn=0; Process p = new Process();//新建一個進程p.StartInfo.Arguments = arg; //進程的命令行參數p.StartInfo.FileName = filepath;//進程啟動路徑p.StartInfo.CreateNoWindow = true;//不顯示新進程的窗口 p.StartInfo.RedirectStandardOutput = true;//輸出重定向p.StartInfo.RedirectStandardError = true; //Redirect the error ouput!p.StartInfo.UseShellExecute = false; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;p.EnableRaisingEvents = true; p.Exited += new EventHandler(p_Exited); //進程自然結束后啟動p—Exited事件p.OutputDataReceived += new DataReceivedEventHandler(ChangeOutput);//進程有輸出時,啟動ChangeOutPut函數 p.Start();//進程啟動p.BeginOutputReadLine();int hstop = WaitHandle.WaitAny(waits);//啟動線程暫停,知道WaitHandle中傳來有效信號switch (hstop)//判斷信號是又哪個{case 0: //進程自然結束if (p.WaitForExit(2000))iReturn = p.ExitCode; //獲取進程的返回值else{CloseProcess();iReturn = -2;}break;case 1: //進程被迫結束p.Kill();//殺掉進程if (!p.HasExited){ p.Kill();} iReturn = -3;break;}HSTOP.Reset(); //HSTOP復位,這個變量指示進程自然結束,每次結束后都得自然復位 p.Close(); //創建的p關閉return iReturn; }private void p_Exited(object sender, EventArgs e){HSTOP.Set();}//輸出重定向函數private void ChangeOutput(object sendingProcess, DataReceivedEventArgs outLine){if (!String.IsNullOrEmpty(outLine.Data)) //字符串不為空時MainForm.FireWriteText(outLine.Data,false);//將進程的輸出信息轉移}
上述代碼基本囊括了對進程Process的操作。在C#工具箱中包括進程這一個組件。
轉載于:https://www.cnblogs.com/liuxiaowei0543/p/3680708.html
總結
以上是生活随笔為你收集整理的C# 进程Process基本的操作说明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 1850 Being a Goo
- 下一篇: C#关键字的个人理解与注释