.NET - WindowStyle = 隐藏 vs. CreateNoWindow = true?

问题描述 投票:0回答:3

当我开始一个新流程时,如果我使用

有什么区别
WindowStyle = Hidden

CreateNoWindow = true

ProcessStartInfo
类的属性?

c# .net process processstartinfo
3个回答
91
投票

正如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); 

24
投票

CreateNoWindow 仅适用于控制台模式应用程序,它不会创建控制台窗口。

WindowStyle 仅适用于本机 Windows GUI 应用程序。它是传递给此类程序的 WinMain() 入口点 的提示。第四个参数,nCmdShow,告诉它如何显示其主窗口。这与桌面快捷方式中的“运行”设置显示的提示相同。请注意,“隐藏”不是一个选项,很少有正确设计的 Windows 程序会满足该请求。由于这对用户造成了影响,他们无法再激活该程序,只能使用任务管理器将其杀死。


16
投票

使用Reflector,如果设置了

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;
© www.soinside.com 2019 - 2024. All rights reserved.