C#对线程的操作
Process 类
提供对本地和远程进程的访问并使您能够启动和停止本地系统进程。
一、根据进程名获取对象
1 /// <summary> 2 /// 根据进程名获取Process对象 3 /// </summary> 4 /// <param name="参数_进程名称"></param> 5 /// <returns></returns> 6 public static Process findProcessObjByProcessName(string f_strProcessName) 7 { 8 Process[] processes; 9 //Get the list of current active processes. 10 processes = System.Diagnostics.Process.GetProcesses();//获取说有进程 11 //Grab some basic information for each process. 12 for (int i = 0; i < processes.Length - 1; i++) 13 { 14 if (f_strProcessName.Equals(processes[i].ProcessName))//Equals:确定两个 String 对象是否具有相同的值。 15 { 16 return processes[i]; 17 } 18 } 19 return null; 20 }
二、模糊查找进程对象:
1 /// <summary> 2 /// 模糊查找进程对象 3 /// </summary> 4 /// <param name="f_strProcessName"></param> 5 /// <returns></returns> 6 public static List<Process> findProcessObjByFuzzyLookup(string f_strProcessName) 7 { 8 Process[] processes; 9 List<Process> result = new List<Process>(); 10 //Get the list of current active processes. 11 processes = System.Diagnostics.Process.GetProcesses(); 12 //Grab some basic information for each process. 13 for (int i = 0; i < processes.Length - 1; i++) 14 { 15 if (processes[i].ProcessName.Contains(f_strProcessName)) 16 { 17 result.Add(processes[i]); 18 } 19 } 20 return result; 21 }
三、进程的新建和终止的操作过程
1 public int CallPhoneExe(string arg) //arg为进程的命令行参数 2 { WaitHandle[] waits =new WaitHandle[2]; //定义两个WaitHandle值,用以控制进程的执行过程 3 waits[0] = HSTOP; //AutoResetEvent HSTOP = new AutoResetEvent(false); 4 waits[1] = GlobalStop;//AutoResetEvent GlobalStop = new AutoResetEvent(false); 5 int iReturn=0; 6 Process p = new Process();//新建一个进程 7 p.StartInfo.Arguments = arg; //进程的命令行参数 8 p.StartInfo.FileName = filepath;//进程启动路径 9 10 p.StartInfo.CreateNoWindow = true;//不显示新进程的窗口 11 p.StartInfo.RedirectStandardOutput = true;//输出重定向 12 p.StartInfo.RedirectStandardError = true; //Redirect the error ouput! 13 p.StartInfo.UseShellExecute = false; 14 p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 15 16 p.EnableRaisingEvents = true; 17 p.Exited += new EventHandler(p_Exited); //进程自然结束后启动p—Exited事件 18 p.OutputDataReceived += new DataReceivedEventHandler(ChangeOutput);//进程有输出时,启动ChangeOutPut函数 19 20 p.Start();//进程启动 21 p.BeginOutputReadLine(); 22 23 int hstop = WaitHandle.WaitAny(waits);//启动线程暂停,知道WaitHandle中传来有效信号 24 switch (hstop)//判断信号是又哪个 25 { 26 case 0: //进程自然结束 27 if (p.WaitForExit(2000)) 28 iReturn = p.ExitCode; //获取进程的返回值 29 else 30 { 31 CloseProcess(); 32 iReturn = -2; 33 } 34 break; 35 case 1: //进程被迫结束 36 p.Kill();//杀掉进程 37 if (!p.HasExited) 38 { 39 p.Kill(); 40 } 41 iReturn = -3; 42 break; 43 } 44 HSTOP.Reset(); //HSTOP复位,这个变量指示进程自然结束,每次结束后都得自然复位 45 p.Close(); //创建的p关闭 46 return iReturn; 47 } 48 49 50 private void p_Exited(object sender, EventArgs e) 51 { 52 HSTOP.Set(); 53 } 54 55 //输出重定向函数 56 private void ChangeOutput(object sendingProcess, DataReceivedEventArgs outLine) 57 { 58 if (!String.IsNullOrEmpty(outLine.Data)) //字符串不为空时 59 MainForm.FireWriteText(outLine.Data,false);//将进程的输出信息转移 60 }