C# 进程Process基本的操作说明
生活随笔
收集整理的這篇文章主要介紹了
C# 进程Process基本的操作说明
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
public int CallPhoneExe(string arg) //arg為進(jìn)程的命令行參數(shù){
WaitHandle[] waits =new WaitHandle[2]; //定義兩個(gè)WaitHandle值,用以控制進(jìn)程的執(zhí)行過程waits[0] = HSTOP; //AutoResetEvent HSTOP = new AutoResetEvent(false);waits[1] = GlobalStop;//AutoResetEvent GlobalStop = new AutoResetEvent(false);int iReturn=0; Process p = new Process();//新建一個(gè)進(jìn)程p.StartInfo.Arguments = arg; //進(jìn)程的命令行參數(shù)p.StartInfo.FileName = filepath;//進(jìn)程啟動(dòng)路徑p.StartInfo.CreateNoWindow = true;//不顯示新進(jìn)程的窗口 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); //進(jìn)程自然結(jié)束后啟動(dòng)p—Exited事件p.OutputDataReceived += new DataReceivedEventHandler(ChangeOutput);//進(jìn)程有輸出時(shí),啟動(dòng)ChangeOutPut函數(shù) p.Start();//進(jìn)程啟動(dòng)p.BeginOutputReadLine();int hstop = WaitHandle.WaitAny(waits);//啟動(dòng)線程暫停,知道WaitHandle中傳來有效信號(hào)switch (hstop)//判斷信號(hào)是又哪個(gè){case 0: //進(jìn)程自然結(jié)束if (p.WaitForExit(2000))iReturn = p.ExitCode; //獲取進(jìn)程的返回值else{CloseProcess();iReturn = -2;}break;case 1: //進(jìn)程被迫結(jié)束p.Kill();//殺掉進(jìn)程if (!p.HasExited){ p.Kill();} iReturn = -3;break;}HSTOP.Reset(); //HSTOP復(fù)位,這個(gè)變量指示進(jìn)程自然結(jié)束,每次結(jié)束后都得自然復(fù)位 p.Close(); //創(chuàng)建的p關(guān)閉return iReturn; }private void p_Exited(object sender, EventArgs e){HSTOP.Set();}//輸出重定向函數(shù)private void ChangeOutput(object sendingProcess, DataReceivedEventArgs outLine){if (!String.IsNullOrEmpty(outLine.Data)) //字符串不為空時(shí)MainForm.FireWriteText(outLine.Data,false);//將進(jìn)程的輸出信息轉(zhuǎn)移}
WaitHandle[] waits =new WaitHandle[2]; //定義兩個(gè)WaitHandle值,用以控制進(jìn)程的執(zhí)行過程waits[0] = HSTOP; //AutoResetEvent HSTOP = new AutoResetEvent(false);waits[1] = GlobalStop;//AutoResetEvent GlobalStop = new AutoResetEvent(false);int iReturn=0; Process p = new Process();//新建一個(gè)進(jìn)程p.StartInfo.Arguments = arg; //進(jìn)程的命令行參數(shù)p.StartInfo.FileName = filepath;//進(jìn)程啟動(dòng)路徑p.StartInfo.CreateNoWindow = true;//不顯示新進(jìn)程的窗口 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); //進(jìn)程自然結(jié)束后啟動(dòng)p—Exited事件p.OutputDataReceived += new DataReceivedEventHandler(ChangeOutput);//進(jìn)程有輸出時(shí),啟動(dòng)ChangeOutPut函數(shù) p.Start();//進(jìn)程啟動(dòng)p.BeginOutputReadLine();int hstop = WaitHandle.WaitAny(waits);//啟動(dòng)線程暫停,知道WaitHandle中傳來有效信號(hào)switch (hstop)//判斷信號(hào)是又哪個(gè){case 0: //進(jìn)程自然結(jié)束if (p.WaitForExit(2000))iReturn = p.ExitCode; //獲取進(jìn)程的返回值else{CloseProcess();iReturn = -2;}break;case 1: //進(jìn)程被迫結(jié)束p.Kill();//殺掉進(jìn)程if (!p.HasExited){ p.Kill();} iReturn = -3;break;}HSTOP.Reset(); //HSTOP復(fù)位,這個(gè)變量指示進(jìn)程自然結(jié)束,每次結(jié)束后都得自然復(fù)位 p.Close(); //創(chuàng)建的p關(guān)閉return iReturn; }private void p_Exited(object sender, EventArgs e){HSTOP.Set();}//輸出重定向函數(shù)private void ChangeOutput(object sendingProcess, DataReceivedEventArgs outLine){if (!String.IsNullOrEmpty(outLine.Data)) //字符串不為空時(shí)MainForm.FireWriteText(outLine.Data,false);//將進(jìn)程的輸出信息轉(zhuǎn)移}
上述代碼基本囊括了對(duì)進(jìn)程Process的操作。在C#工具箱中包括進(jìn)程這一個(gè)組件。
轉(zhuǎn)載于:https://www.cnblogs.com/liuxiaowei0543/p/3680708.html
總結(jié)
以上是生活随笔為你收集整理的C# 进程Process基本的操作说明的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 1850 Being a Goo
- 下一篇: JavaScript学习05 定时器