What are the differences between a murder process and a closed process?

When I start a process and want to close this process, what are the differences between Process.Close() and Process.Kill() ?

I asked because I have an application that starts to capture a packet using Wireshark using a command through the command line with Windows = hidden . therefore, when I want to stop the capture, I will kill this process. Sometimes, sometimes the capture opens with an error that the last packet was cut in the middle, so I wonder if it can use close() before kill() solves this problem?

When I start to capture, I can close it by pressing Ctrl + C, but in my case I will open the window in a hidden state. Can I do something similar using my code?

+42
c # process
Dec 19 '12 at 12:37
source share
5 answers

What is the difference between Process.Close() and process.Kill() ?

The manual is clear enough:

Process.Close() :

The Close method causes the process to stop waiting for exit if it waits, closes the process handle and clears the process-specific properties. Close does not close standard output, input, and error readers and writers if they are referenced externally. [Those. the process itself continues to work, you simply cannot control it using your Process instance

process.Kill() :

Kills the forced termination of the process, and CloseMainWindow requests only the completion. [...] A request to exit the process by calling CloseMainWindow does not force the application to exit. An application may request a user check before logging out or may refuse to refuse. To force the application to exit, use the Kill method. The behavior of CloseMainWindow is identical to the behavior of the user closing the main application window using the system menu. Therefore, a request to exit the process by closing the main window does not force the application to immediately stop working.

Process.CloseMainWindow :

Closes a process that has a user interface by sending a tag message to the main window.




As for your editing:

I asked because I have an application that starts to capture a packet using wirehark with a command through the command line with Windows = hidden.

Use the WinPcap API or the Pcap.Net library for this , not Wireshark.

+46
Dec 19 '12 at 12:50
source share

Kill requests the system to unceremoniously terminate the process (by calling the Close system closes the process handle in .NET, but does not actually affect the process itself. Close also called Dispose . See this answer from Jon Skeet.

The third option is CloseMainWindow , which gracefully closes the program, asking to close the main window. It is like right-clicking on a program in the taskbar and selecting Close.

+9
Dec 19 '12 at 12:41
source share

Kill and close are not two options. Kill (or CloseMainWindow) must be called if you want to complete the process. Calling Close will free up resources, but it will not close standard threads. Closing threads on their own may or may not lead to the completion of the process, it depends on the process. A better strategy is to call Kill or CloseMainWindow depending on the type of process, and then Dispose the Process object.

Close begins to close the Process object. It is called Dispose. Close is typically used to manage the resources of a Process object.

Kill completes the process. This is the only way to end a process that does not have a user interface.

CloseMainWindow sends a closed message to the main window of the process. This is an elegant way to stop the user interface process.

+2
Dec 19 '12 at 12:50
source share

Process.Kill calls TerminateProcess.

Process.Close does not do what you think it does! It just frees the resources used by the class instance. Please read the following: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.close.aspx

+1
Dec 19 '12 at 12:42 on
source share

Let me illustrate what happens visually when you call Close () and Kill () . Next, Notepad will open, but it will not exit the notepad at the end when myProcess.Close () is executed.

 Process myProcess; myProcess = Process.Start("Notepad.exe"); Thread.Sleep(10000); myProcess.Close(); 

Notepad opens. after 10 seconds, the notepad will close at the end when myProcess.Kill () executes.

 Process myProcess; myProcess = Process.Start("Notepad.exe"); Thread.Sleep(10000); myProcess.Kill(); 
+1
Sep 02 '14 at 16:31
source share



All Articles