【发布时间】:2011-07-02 21:57:03
【问题描述】:
当我开始一个新进程时,如果我使用它有什么不同
WindowStyle = Hidden
或
CreateNoWindow = true
ProcessStartInfo 类的属性?
【问题讨论】:
标签: c# .net process processstartinfo
当我开始一个新进程时,如果我使用它有什么不同
WindowStyle = Hidden
或
CreateNoWindow = true
ProcessStartInfo 类的属性?
【问题讨论】:
标签: c# .net process processstartinfo
CreateNoWindow 仅适用于控制台模式应用程序,它不会创建控制台窗口。
WindowStyle 仅适用于本机 Windows GUI 应用程序。这是传递给此类程序的WinMain() entry point 的提示。第四个参数,nCmdShow,告诉它如何显示它的主窗口。这与桌面快捷方式中的“运行”设置显示的提示相同。请注意,“隐藏”不是一个选项,很少有适当设计的 Windows 程序满足该请求。由于这会欺骗用户,因此他无法再激活该程序,只能使用任务管理器将其杀死。
【讨论】:
正如 Hans 所说,WindowStyle 是传递给进程的建议,应用程序可以选择忽略它。
CreateNoWindow 控制控制台如何为子进程工作,但它不能单独工作。
CreateNoWindow 与 UseShellExecute 结合使用如下:
要在没有任何窗口的情况下运行该进程:
ProcessStartInfo info = new ProcessStartInfo(fileName, arg);
info.CreateNoWindow = true;
info.UseShellExecute = false;
Process processChild = Process.Start(info);
在自己的窗口中运行子进程(新控制台)
ProcessStartInfo info = new ProcessStartInfo(fileName, arg);
info.UseShellExecute = true; // which is the default value.
Process processChild = Process.Start(info); // separate window
在父控制台窗口中运行子进程
ProcessStartInfo info = new ProcessStartInfo(fileName, arg);
info.UseShellExecute = false; // causes consoles to share window
Process processChild = Process.Start(info);
【讨论】:
Process process = Process.Start(psi); 而不是 process.Start() 我不相信存在任何行为差异,并且您仍在创建流程之前设置 StartInfo。跨度>
使用反射器,如果设置了UseShellExecute,则使用WindowStyle,否则使用CreateNoWindow。
在 MSDN 的示例中,您可以看到他们是如何设置的:
// Using CreateNoWindow requires UseShellExecute to be false
myProcess.StartInfo.UseShellExecute = false;
// You can start any process, HelloWorld is a do-nothing example.
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
在另一个示例中,它位于下方,因为 UseShellExecute 默认为 true
// UseShellExecute defaults to true, so use the WindowStyle
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
【讨论】: