Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - how to check if a process was successfully started [duplicate]

Tags:

c#

process

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.

like image 636
Simon Avatar asked Jun 19 '12 08:06

Simon


1 Answers

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
        }
like image 90
Oscar Foley Avatar answered Oct 20 '22 01:10

Oscar Foley