通过调用Process.Kill()方法来终止一个进程,该方法会立即终止进程,不会等待进程完成任何未完成的工作。示例如下:
Process myProcess = new Process();myProcess.StartInfo.FileName = "notepad.exe";myProcess.Start();// 终止进程myProcess.Kill();通过调用Process.Close()方法来终止一个进程,该方法会发送关闭信号给进程,但是进程可以选择是否要响应。示例如下:Process myProcess = new Process();myProcess.StartInfo.FileName = "notepad.exe";myProcess.Start();// 终止进程myProcess.CloseMainWindow();通过调用Process.WaitForExit()方法来等待进程执行完成后再终止进程,该方法会一直等待直到进程执行完成或超时。示例如下:Process myProcess = new Process();myProcess.StartInfo.FileName = "notepad.exe";myProcess.Start();// 等待进程执行完成myProcess.WaitForExit();使用Windows API函数TerminateProcess来强制结束一个进程,示例如下:[DllImport("kernel32.dll")]public static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode);Process myProcess = new Process();myProcess.StartInfo.FileName = "notepad.exe";myProcess.Start();// 获取进程句柄,并强制结束进程TerminateProcess(myProcess.Handle, 0);需要注意的是,强制结束一个进程可能会导致数据丢失或者系统不稳定,因此建议谨慎使用。


