tags:

views:

25

answers:

2

Given a particular System.Diagnostics.Process that has been assigned values to its properties .FileName and .Arguments, what is the best way to determine if it:

  1. has ever been issued a .Start() command or
  2. is currently associated with a process at all?

If .Start() was never issued, then calling .CloseMainWindow() causes an InvalidOperationException, which I'd like to avoid.

A: 

Have you tried checking Process.Id and one of the Exit properties ?

Ref.

In response to poster's comments: perhaps you will have to wrap in a try/catch and an exception being thrown indicates not started? It's not pretty!

Mitch Wheat
`Id`, `ExitCode` and `ExitTime` all share the same exception, unfortunately.
JYelton
The try/catch is what's working now and apparently what we'll stick to. :)
JYelton
A: 

You could try and check

process.StartTime & process.HasExited
process.Handle

or perhaps use

process.WaitForExit
Greg Domjan
In the debugger, nearly every property including `StartTime`, `HasExited`, and `Id` are all showing the same exception and I cannot check them as a means to determine if a process is associated. `WaitForExit()` also throws the same exception.
JYelton