C # - how to check if a process has been started successfully

Possible duplicate:
How do I know if Process.Start () is successful?

I have a process that looks like a watchdog timer (let it be called WD) in my program, which is another running process (call it A). I am starting a process WDin a specific event, let it be said that a key is pressed, and I want to start another process using this process, let me call it B.

The fact is that I want to complete the initial process Aafter I know that the process Bhas been started successfully. How can I check this?

I begin the process WDand Busing the syntax Process.Start(argList)and ProcessInfo(argList).

Each process is a simple C # console application.

+5
source share
2 answers

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

Your code should look something like this:

        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
        }
+4
source

Process.start

returns: true if the process resource is running; false if a new process resource is not running (for example, if an existing process is reused).

I would suggest that you should just check the return value Process.Start. If it is true, you can close the current process.

null, .

0

All Articles