简体   繁体   English

以下流程有什么区别?

[英]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() . 对我来说,两者似乎都是相同的process.start() So what is the difference? 那么区别是什么呢?

They are very similar but not equivalent. 它们非常相似但相同。

Here is how Process.Start method implemented ; 以下是Process.Start方法的 实现 方法 ;

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. new ProcessStartInfo(fileName, arguments)构造函数将第二个参数设置为参数字符串,即ProcessStartInfo.Arguments属性而不是Verb属性。 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. 重载将资源与新的Process组件相关联。 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. 相反,重用现有流程资源,不会创建新的Process组件。 In such a case, instead of returning a new Process component, Start returns null to the calling procedure. 在这种情况下, Start不会返回新的Process组件,而是向调用过程返回null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM