C# – how to check if a process was successfully started

cprocess

Possible Duplicate:
How to know if Process.Start() is successful?

I have a process similar to a watchdog(let's call it WD) in my program which is another running process(let's call it A). I am starting WD process at a certain event, let's say a key is pressed and I want to start another process with this process, let's call it B.

The thing is that I want to shut down the initial process A after what I know that process B was successfully started. How can I check that?

I am starting process WD and B with Process.Start(argList) and ProcessInfo(argList) syntax.

Each process is a simple C# Console Application.

Best Answer

Process.Start returns a boolean (true if process started correctly) Check this MSDN link for Process.Start() method.

Your code should be something like:

        Process B= new Process();

        try
        {
            B.StartInfo.UseShellExecute = false;
            B.StartInfo.FileName = "C:\\B.exe";
            B.StartInfo.CreateNoWindow = true;
            if (B.Start())
            {
              // Kill process A 
            }
            else
            {
               // Handle incorrect start of process B and do NOT stop A
            }

        }
        catch (Exception e)
        {
            // Handle exception and do NOT stop A
        }