C# 杀死进程和关闭进程有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13952635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the differences between kill process and close process?
提问by user1710944
When I start a process and want to close this process, what are the differences between Process.Close()
and Process.Kill()
?
当我开始一个进程并想关闭这个进程时,Process.Close()
和之间有什么区别Process.Kill()
?
I asked because I have an application which starts to capture packet using Wireshark with a command via command line with Windows = hidden
.
so when I want to stop the capturing I kill the process .So sometimes the capture opens with an error that the last packet was cut in the middle so I am wondering can using close()
before kill()
will solve this issue ?
我问是因为我有一个应用程序,它开始使用 Wireshark 捕获数据包,并通过命令行使用Windows = hidden
. 所以当我想停止捕获时,我会终止进程。所以有时捕获打开时会出现一个错误,即最后一个数据包被截断在中间,所以我想知道使用close()
beforekill()
可以解决这个问题吗?
When I start capturing I can close it by pressing Ctrl + C but in my case I open the window in hidden state, Can I do something similar via my code ?
当我开始捕获时,我可以按 Ctrl + C 关闭它,但在我的情况下,我以隐藏状态打开窗口,我可以通过我的代码做类似的事情吗?
采纳答案by CodeCaster
What are the differences between
Process.Close()
andprocess.Kill()
?
Process.Close()
和之间有什么区别process.Kill()
?
The manual is pretty clear on that:
手册对此非常清楚:
The Close method causes the process to stop waiting for exit if it was waiting, closes the process handle, and clears process-specific properties. Close does not close the standard output, input, and error readers and writers in case they are being referenced externally. [i.e. the process itself keeps running, you just cannot control it anymore using your
Process
instance]
Close 方法会导致进程在等待退出时停止等待,关闭进程句柄并清除进程特定的属性。Close 不会关闭标准输出、输入和错误读取器和写入器,以防它们被外部引用。[即进程本身一直在运行,你只是不能再使用你的
Process
实例来控制它]
Kill forces a termination of the process, while CloseMainWindow only requests a termination. [...] The request to exit the process by calling CloseMainWindow does not force the application to quit. The application can ask for user verification before quitting, or it can refuse to quit. To force the application to quit, use the Kill method. The behavior of CloseMainWindow is identical to that of a user closing an application's main window using the system menu. Therefore, the request to exit the process by closing the main window does not force the application to quit immediately.
Kill 强制终止进程,而 CloseMainWindow 仅请求终止。[...] 通过调用 CloseMainWindow 退出进程的请求不会强制应用程序退出。应用程序可以在退出前要求用户验证,也可以拒绝退出。要强制退出应用程序,请使用 Kill 方法。CloseMainWindow 的行为与用户使用系统菜单关闭应用程序主窗口的行为相同。因此,通过关闭主窗口退出进程的请求不会强制应用程序立即退出。
Closes a process that has a user interface by sending a close message to its main window.
通过向主窗口发送关闭消息来关闭具有用户界面的进程。
As for your edit:
至于你的编辑:
i asked because i have application who start to capture packet using wireshark with command via command line with Windows = hidden.
我问是因为我有应用程序开始使用带有命令的wireshark通过命令行与Windows = hidden一起捕获数据包。
Use the WinPcap APIor the Pcap.Net library for that, not Wireshark.
为此使用 WinPcap API或Pcap.Net 库,而不是 Wireshark。
回答by David Pfeffer
Kill
asks the systemto unceremoniously terminate the process (via a call to the Windows TerminateProcesssystem call). This is akin to End Process in Task Manager.
Kill
要求系统毫不客气地终止进程(通过调用 Windows TerminateProcess系统调用)。这类似于任务管理器中的结束进程。
Closecloses the handle to the process in .NET, but does not actually touch the process itself. Close
is also called by Dispose
. See this answerby Jon Skeet.
Close在 .NET 中关闭进程的句柄,但实际上并不触及进程本身。Close
也称为Dispose
。请参阅Jon Skeet 的这个答案。
A third option is CloseMainWindow, which doesgracefully shut down the program by asking the primary window to close. This is akin to right-clicking the program on the task bar and choosing Close.
第三种选择是CloseMainWindow,这不正常通过询问主窗口接近关闭程序。这类似于右键单击任务栏上的程序并选择关闭。
回答by wj32
Process.Kill calls TerminateProcess.
Process.Kill 调用 TerminateProcess。
Process.Close doesn't do what you think it does! It simply frees resources used by the instance of the class. Please read this: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.close.aspx
Process.Close 不会像你想象的那样做!它只是释放类实例使用的资源。请阅读:http: //msdn.microsoft.com/en-us/library/system.diagnostics.process.close.aspx
回答by Fls'Zen
Kill and Close are not two options. Kill (or CloseMainWindow) must be called if you want to terminate a process. Calling Close will just free resources, but it won't close the standard streams. Closing the streams yourself may or may not cause the process to end, it depends on the process. The best strategy is to call Kill or CloseMainWindow depending on the type of process and then Dispose the Process object.
Kill 和 Close 不是两种选择。如果要终止进程,则必须调用 Kill(或 CloseMainWindow)。调用 Close 只会释放资源,但不会关闭标准流。自己关闭流可能会也可能不会导致过程结束,这取决于过程。最好的策略是根据进程的类型调用 Kill 或 CloseMainWindow,然后 Dispose Process 对象。
Closebegins closing the Process object. It's called by Dispose. Close is generally used for managing the Process object's resources.
Close开始关闭 Process 对象。它由 Dispose 调用。Close 通常用于管理 Process 对象的资源。
Killterminates the process. This is the only way to terminate a process that has no user interface.
Kill终止进程。这是终止没有用户界面的进程的唯一方法。
CloseMainWindowsends a close message to the main window of the process. This is the graceful way to stop a process that has a user interface.
CloseMainWindow向进程的主窗口发送关闭消息。这是停止具有用户界面的进程的优雅方式。
回答by yantaq
Let me illustrate what happens visually when you call Close() and Kill() . The following will open Notepad but does not exit the notepad at the end when myProcess.Close() executes.
让我举例说明当您调用Close() 和 Kill()时会发生什么。以下将打开记事本,但在 myProcess.Close() 执行结束时不会退出记事本。
Process myProcess;
myProcess = Process.Start("Notepad.exe");
Thread.Sleep(10000);
myProcess.Close();
the following will open Notepad. after 10 seconds the notepad will close at the end when myProcess.Kill() executes.
下面将打开记事本。10 秒后,当 myProcess.Kill() 执行时,记事本将在最后关闭。
Process myProcess;
myProcess = Process.Start("Notepad.exe");
Thread.Sleep(10000);
myProcess.Kill();