3

Given the code below, which was an accepted answer to another question.

Is this safe to run new Processes this way or should I manage a reference to MyProcess?

It is my understanding the GC will (eventually) dispose of objects which no longer have a reference. With MyProcess going out of scope, wouldn't the Dispose() method be invoked when this reference is eventually Garbage Collected? My impression would be that it may run for a while, up until the GC does a collection.

void TestMethod()
{
    Process MyProcess = new Process();
    MyProcess.StartInfo = new ProcessStartInfo("app.exe");
    MyProcess.StartInfo.WorkingDirectory = "";
    MyProcess.StartInfo.Arguments = "some arguments";
    MyProcess.Start();
}
Community
  • 1
  • 1
Moon
  • 1,141
  • 2
  • 11
  • 25
  • I assume the reference to the process, via MyProcess will be garbage collected. However, the process itself will still continue to run. – Joseph Young Dec 21 '15 at 22:32
  • 1
    A process never "goes out of scope" - once you start a process, it runs (as a separate process) until it ends or someone forcibly terminates it. A `Process` instance may go out of scope but that's not the process itself - it's just a descriptor of the process that lets you get information about a particular process. – xxbbcc Dec 21 '15 at 22:32
  • But wouldn't the Dispose() method be called and end the process? – Moon Dec 21 '15 at 22:33
  • @Don No, `Dispose` gets rid of the resources associated with the in-memory `Process` instance, not the process _described by that instance_. – xxbbcc Dec 21 '15 at 22:34
  • 1
    @Don Look at it this way: your `Process` instance lives independently of the process - you create it before the process even gets started. Once it's started, it lives in a separate memory space. The `Process` instance has the ability to get updated information about the process as long as the instance is in scope - after that it gets cleaned up but that doesn't affect the process. – xxbbcc Dec 21 '15 at 22:36
  • So would it be safe to put a MyProcess.Dispose(); immediately after Start() is called? – Moon Dec 21 '15 at 22:37
  • @Don If you don't care about the process after you start it, yes (use a `using` block instead). If you care about it then you need to use a higher-level scope to keep the `Process` instance around. – xxbbcc Dec 21 '15 at 22:39

2 Answers2

3

Once you start a process, it never goes out of scope - a process gets its own memory space and its own time slices from the operating system and it runs until it decides to quit or someone forcibly terminates it.

A Process instance is a descriptor for a particular process - it's not the process itself but it has the ability to get information about the process and to make certain changes to the process.

The Process instance can be disposed of - this releases the resources needed to query the operating system process but it doesn't actually end the process in question - it just stops the Process instance from being used to access the underlying process anymore.

If you want to use the Process instance to actually stop the underlying process, you can call Kill() but this is dangerous and not recommended - this literally stops the process right away, without letting the process to shut down gracefully (which can lead to all kinds of problems).

To answer your question: there's nothing wrong with the code in your question - it simply starts an operating system process and then throws away the Process instance without using it for anything else. (It'd be possible to use the Process instance to redirect the standard input / output, wait for the process to finish, etc.)

xxbbcc
  • 16,930
  • 5
  • 50
  • 83
2

The Process object is independent of the actual operating system process. Either one can terminate while the other lives on.

If the Process object dies then the operating system process can continue. You just can't interact with it through the now deceased Process object.

If the operating process terminates, then the Process object is still meaningful. For instance, you can query the exit code.

The code in the question is perfectly safe.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490