Search code examples
c#processidisposableusing

What happens to a Process in a using statement without WaitForExit()?


In the following example, what happens to the process if it is still running once the code leaves the using statement?

using (var p = new Process())
{
    p.StartInfo.FileName = "c:\\temp\\SomeConsoleApp.exe";
    p.Start();
}

Solution

  • One should separate the OS process that is running on your system from the Process object that represents a "handle" to it in your program:

    • The process continues running until it completes, or you kill it using OS-specific methods
    • The Process object gets disposed, so your program can no longer interact with the OS process.

    Call of Dispose() method on the Process object does not kill the OS process.