fishmong3r
fishmong3r

Reputation: 1434

What is the difference between the below processes?

This process is running "independently" from my app. I can use my form meanwhile the script is running, not waiting for exit.

string strCmdText = "some command line script";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);

This one though stops the process in my form till command line window is being closed:

Process p = new Process();
p.StartInfo.Verb = "runas";
p.StartInfo.FileName = cmd.exe;
p.Start();

To me both seems to be the same process.start(). So what is the difference?

Upvotes: 3

Views: 386

Answers (1)

Soner Gönül
Soner Gönül

Reputation: 98848

They are very similar but not equivalent.

Here is how Process.Start method implemented;

public static Process Start(string fileName, string arguments)
{
     return Start(new ProcessStartInfo(fileName, arguments));
}

new ProcessStartInfo(fileName, arguments) constructor sets second parameter to arguments string which is ProcessStartInfo.Arguments property not Verb property. And also;

public static Process Start(ProcessStartInfo startInfo)
{
     Process process = new Process();
     if (startInfo == null) throw new ArgumentNullException("startInfo");
     process.StartInfo = startInfo;
     if (process.Start()) {
         return process;
     }
     return null;
}

As you can see from it's documentation;

The overload associates the resource with a new Process component. If the process is already running, no additional process is started. Instead, the existing process resource is reused and no new Process component is created. In such a case, instead of returning a new Process component, Start returns null to the calling procedure.

Upvotes: 2

Related Questions