当我开始一个新进程时,如果我使用
WindowStyle = Hidden
或者
CreateNoWindow = true
类的属性ProcessStartInfo
?
当我开始一个新进程时,如果我使用
WindowStyle = Hidden
或者
CreateNoWindow = true
类的属性ProcessStartInfo
?
正如 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);
CreateNoWindow 仅适用于控制台模式应用程序,它不会创建控制台窗口。
WindowStyle 仅适用于本机 Windows GUI 应用程序。它是传递给此类程序的WinMain() 入口点的提示。第四个参数,nCmdShow,告诉它如何显示它的主窗口。这与桌面快捷方式中的“运行”设置显示的提示相同。请注意,“隐藏”不是一个选项,很少有适当设计的 Windows 程序满足该请求。由于这会影响用户,因此他无法再激活该程序,只能使用任务管理器将其杀死。
WindowStyle
使用反射器,如果设置了,它看起来像使用UseShellExecute
,否则使用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;