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

发布于 2024-10-18 16:51:13 字数 176 浏览 1 评论 0原文

属性有什么区别?

WindowStyle = Hidden

CreateNoWindow = true

当我启动一个新进程时,如果使用ProcessStartInfo 类的

When I start a new process, what difference does it make if I use the

WindowStyle = Hidden

or the

CreateNoWindow = true

property of the ProcessStartInfo class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦等七年七年为一梦 2024-10-25 16:51:13

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

As Hans said, WindowStyle is a recommendation passed to the process, the application can choose to ignore it.

CreateNoWindow controls how the console works for the child process, but it doesn't work alone.

CreateNoWindow works in conjunction with UseShellExecute as follows:

To run the process without any window:

ProcessStartInfo info = new ProcessStartInfo(fileName, arg); 
info.CreateNoWindow = true; 
info.UseShellExecute = false;
Process processChild = Process.Start(info); 

To run the child process in its own window (new console)

ProcessStartInfo info = new ProcessStartInfo(fileName, arg); 
info.UseShellExecute = true; // which is the default value.
Process processChild = Process.Start(info); // separate window

To run the child process in the parent's console window

ProcessStartInfo info = new ProcessStartInfo(fileName, arg); 
info.UseShellExecute = false; // causes consoles to share window 
Process processChild = Process.Start(info); 
轻许诺言 2024-10-25 16:51:13

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

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

CreateNoWindow only applies to console mode apps, it won't create the console window.

WindowStyle only applies to native Windows GUI apps. It is a hint passed to the WinMain() entry point of such a program. Fourth argument, nCmdShow, telling it how to show its main window. This is the same hint that appears as the "Run" setting in a desktop shortcut. Note that "hidden" is not an option there, few properly designed Windows program honor that request. Since that snookers the user, they can't get the program activated anymore and can only kill it with Task Manager.

匿名。 2024-10-25 16:51:13

使用 Reflector,它看起来像 WindowStyleUseShellExecute<,则使用 >/code> 已设置,否则使用 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;

Using Reflector, it looks like WindowStyle is used if UseShellExecute is set, otherwise it uses CreateNoWindow.

In MSDN's example, you can see how they set it:

// 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();

In the other example, its just below because UseShellExecute is defaulted to true

// UseShellExecute defaults to true, so use the WindowStyle
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文