Process.Start vs Process `p = new Process ()` in C #?

As indicated in this post , there are two ways to call another process in C #.

Process.Start("hello");

and

Process p = new Process();
p.StartInfo.FileName = "hello.exe";
p.Start();
p.WaitForExit();
  • Q1: What are the advantages / disadvantages of each approach?
  • Q2: How to check if an error occurs with a method Process.Start()?
+3
source share
3 answers

Using the first method you cannot use WaitForExit, since the method returns null if the process is already running.

How do you check if a new process has been started, different methods. The first returns an object Processor null:

Process p = Process.Start("hello");
if (p != null) {
  // A new process was started
  // Here it possible to wait for it to end:
  p.WaitForExit();
} else {
  // The process was already running
}

The second returns bool:

Process p = new Process();
p.StartInfo.FileName = "hello.exe";
bool s = p.Start();
if (s) {
  // A new process was started
} else {
  // The process was already running
}
p.WaitForExit();
+6
source

. , ( , shell-exec ..) ProcessStartInfo, Process.Start(ProcessStartInfo).

; Process.Start Process, , . stderr, , , , ProcessStartInfo.

+6

Very few differences. The static method returns a process object, so you can still use "p.WaitForExit ()", etc. - Using the method in which you create a new process, it would be easier to change the process parameters (affinity for the processor, etc.) before starting the process.

Other than that, there is no difference. A new process object is created in both directions.

In your second example, this is identical to this:

Process p = Process.Start("hello.exe");
p.WaitForExit();
+1
source

Source: https://habr.com/ru/post/1791657/


All Articles