Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to use Process.WaitForExit after Process.Kill?

Is it necessary to use Process.WaitForExit after Process.Kill?
What if the calling process exits right after it calls Process.Kill?
Would this cause Process.Kill to fail?

Edit: I need to kill a process on exiting my application. At this point I do not intend to handle the situations when kill fails so it is not necessary for me to wait for the process to exit. So if calling WaitForExit is not necessary I could just skip it.

like image 243
axk Avatar asked Aug 16 '11 12:08

axk


People also ask

What does process WaitForExit do?

Process.WaitForExit Method (System.Diagnostics)Sets the period of time to wait for the associated process to exit, and blocks the current thread of execution until the time has elapsed or the process has exited.

What is WaitForExit in c#?

WaitForExit() Instructs the Process component to wait indefinitely for the associated process to exit. public: void WaitForExit(); C# Copy.

How do you terminate a process in C#?

You can use Process. GetProcesses() to get the currently running processes, then Process. Kill() to kill a process.


1 Answers

According to the documentation on MSDN (emphasis mine)

Note The Kill method executes asynchronously. After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine if the process has exited.

If you don't need to wait, of if you don't need to determine if the process actually exists, then I suppose you don't need to call WaitForExit or check HasExited.

like image 88
David Ruttka Avatar answered Oct 16 '22 22:10

David Ruttka