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

后端 未结 4 1256
温柔的废话
温柔的废话 2020-12-10 10:09

I\'ve tried two different methods for starting a process.

The first

The definition is defined as parameters to the Start method:

4条回答
  •  一个人的身影
    2020-12-10 10:55

    Process.Start() also returns a boolean value that let's you know if it acquired an existing process or if a new process was started.

    Furthermore, you can check the ProcessId of the process to ensure it's started/still running. Something like:

    bool started = False;
    Process p = new Process();
    p.StartInfo = YourStartInfo;
    started = p.Start();
    
    try {
      int procId = p.Id;
    }
    catch(InvalidOperationException){
      started = False
    }
    catch(Exception ex) {
      started = False
    }
    

提交回复
热议问题