C#我无法使CreateNoWindow正常工作-甚至msdn.com示例都没有 [英] C# I can't get CreateNoWindow to work - not even the msdn.com example

查看:80
本文介绍了C#我无法使CreateNoWindow正常工作-甚至msdn.com示例都没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这里

https://msdn.microsoft.com/zh-CN/library/system.diagnostics.processstartinfo.createnowindow(v = vs.110).aspx

我知道

A)并非每个程序都尊重它的CreateNoWindow请求,并且

A) Not every program respects it CreateNoWindow request and

B)它要求UseShellExecute = False(默认为true)。

B) It requires UseShellExecute=False (default is true).

C)用于指示是否创建了窗口。 (它永远不会隐藏父窗口)。谈到是否为子进程创建一个窗口。 (msdn说新窗口)

C) It is meant to indicate whether a window is created. (it won't ever hide the parent window). It's speaking of whether a window is created for the child process. (msdn says 'new window')

D)CreateNoWindow默认为false。

D) CreateNoWindow defaults to false.

我正在从cmd运行此程序,尽管在Visual Studio中单击播放可以说明相同的行为。

I am running this from cmd, though clicking play in visual studio illustrates the same behaviour.

我设置了CreateNoWindow为false,所以我以为它将为子进程创建一个新窗口。

I set CreateNoWindow to false so i'd have thought it'd create a new window for the child process.

但事实并非如此。

任务管理器显示了cmd.exe的第二个实例,但是仍然可以看到一个窗口。下面是来自一个窗口的粘贴。它已将子cmd.exe加载到父窗口(一个cmd窗口)中。

task manager shows the second instance of cmd.exe but there's still the one window as you can see. Below is the paste from one window. It has loaded the child cmd.exe in the parent window (the one cmd window)

如果我将CreateNoWindow设置为true或false,则似乎没有什么区别。

If I set CreateNoWindow to true or false, then it seems to make no difference.

所以我显然没有在该msdn链接上工作的示例。

So i'm clearly not getting the example at that msdn link to work.

C:\crp>type aaa.csc
using System.Diagnostics;

class blah{
   public static void Main(string[] args) {
          Process p = new Process();
          ProcessStartInfo psi = new ProcessStartInfo();
          psi.UseShellExecute=false; 
          psi.FileName="cmd.exe";
          psi.CreateNoWindow=false;
          p.StartInfo=psi;
          p.Start();
   }
}
C:\crp>csc aaa.csc
Microsoft (R) Visual C# Compiler version 4.0.30319.34209
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.


C:\crp>aaa.exe

C:\crp>Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\crp>

更新

看来确实可行。如果打开cmd提示符并运行exe,则在某些情况下,它将在父级文件中启动cmd.exe。在其他情况下,它将启动没有任何窗口的cmd.exe。虽然在Visual Studio中的行为似乎很奇怪,请参阅我对ephraim的回答。在Visual Studio中的此问题中,我仅将项目设置为控制台应用程序进行了尝试,并且cmd提示窗口始终至少会闪烁。对于非控制台应用程序来说,行为要简单得多。

It seems it does work. If you open a cmd prompt and run the exe, then in one case it starts cmd.exe within the parent. In the other case it starts a cmd.exe without any window. The behaviour within visual studio seems odd though, see my comment to ephraim's answer. In this question while within visual studio, i'd only tried it with the project set to being a console application, and there a cmd prompt window will always at least flick up. Behaviour is much simpler for a non console application e.g. a winforms application.

推荐答案

首先尝试从cmd.exe进行尝试最有意义。

It makes most sense to first try from a cmd.exe

然后从那里了解在Visual Studio中运行时的行为

then from there understand the behaviour when running from visual studio

CreateNoNewWindow中的new一词有点误导。

The word new in CreateNoNewWindow, is a bit misleading. It's more like it creates no window and even uses no window.

如果您具有CreateNoNewWindow = false(默认值),则它将在父窗口中运行该子cmd 。 (这可能看起来很奇怪,但是如果将其与CreateNoNewWindow = true时发生的情况进行比较,则并不奇怪)。运行该程序时,您会看到它显示带有版权e.t.c的cmd标语。

If you have CreateNoNewWindow=false (the default), then it will run that child cmd in the parent window. (that may seem strange but if you compare it to what happens when CreateNoNewWindow=true then it's not so strange). When you run the program you'll see it says the cmd banner with Copyright e.t.c. that's from the child cmd running in the parent window.

如果您具有CreateNoNewWindow = true并运行程序,则它将不会输出任何内容。任务管理器仍将显示第二个cmd.exe,这是因为它正在运行子cmd.exe,但根本不在任何cmd窗口中运行。

If you have CreateNoNewWindow=true and you run your program then it will output nothing. Task manager will still show the second cmd.exe that is because it is running the child cmd.exe but not in any cmd window at all.

视觉工作室。

使用Visual Studio控制台应用程序,当您单击播放时,Visual Studio会弹出其cmd窗口,一旦程序完成,该窗口就会消失,但是任何生成的进程都可以运行上。

With Visual studio console applications when you click play, visual studio will pop up its cmd window which disappears once the program has completed, but any spawned processes would live on.

对于Visual Studio,如果您将CreateNoNewWindow设置为true(即完全不使用任何窗口),则它将弹出其cmd窗口,以no方式生成第二个cmd窗口,因此它是不可见的,然后父cmd窗口将关闭,并且生成的子cmd.exe将继续在任务管理器中显示,但没有窗口。因此,任务管理器将显示一个cmd.exe,并且您不会看到任何窗口。

With visual studio, If you have CreateNoNewWindow set to true (i.e. use no window at all), then it will pop up its cmd window, spawn the second cmd in no window, so it's invisible, then the parent cmd window will close and the spawned child cmd.exe will continue to show in task manager but will have no window. So task manager will show one cmd.exe, and you'll see no window.

作为测试以下段落之前的测试,您可以创建一个cmd窗口 sdfsdfds 使用向上箭头可以访问命令历史记录,因此您可以查看自己是否处于parenet命令中。然后开始一个子进程。键入 cmd< ENTER> 。现在杀死父母。孩子仍然在那儿。

As a test before testing the paragraph below, you can create a cmd window, sdfsdfds so you have something in command history accessible with up arrow so you can see whether you're in the parenet command or not. Then start a child process. type cmd<ENTER>. Now kill the parent. The child is still there.

对于Visual Studio,如果您将CreateNoNewWindow设置为false(即使用父窗口),(如我们所见,这是可能的即使在父进程被杀死之后,如果在父进程被杀死之前创建了孩子并使用该窗口,则单击播放,然后Visual Studio会像平移一样向上滑动其cmd窗口,并生成一个子cmd。 exe进程和任务管理器仅在窗口中显示一个cmd.exe进程,并且播​​放按钮再次显示为活动状态,因此该程序必须已完成执行,并且父进程已完成。因此,Visual Studio关闭了父cmd.exe,但是孩子仍然在使用父窗口,就像在完成父cmd.exe之前一样。

With visual studio, if you have CreateNoNewWindow set to false(i.e. use the parent window), (this, as we have seen, is possible even after a parent process has been killed, if the child was created and using that window, before the parent was killed) then click play, then visual studio will flick up its cmd window as it does, and it will spawn a child cmd.exe process and task manager only shows one cmd.exe process but in a window, and the play button shows as active again, so the program must have completed execution, and the parent process was completed. So visual studio closed the parent cmd.exe but the child is still there still using the parent's window as it was prior to the parent cmd.exe being completed.

当将Visual Studio项目设置为控制台应用程序时,控制台总是至少会弹起。

That's for when a visual studio project is set to console application, where a console will always at least flick up.

但是对于未创建为控制台应用程序的项目,例如对于Winforms应用程序,可以将控制台设置为隐藏,并且即使对于主程序,也没有控制台可以弹起或根本不需要运行。

But for a project that isn't created as a console application e.g. for a winforms application, the console can be set to hide, and no console would flick up or need to be run at all even for the main program.

请注意此处- UseShellExecute和CreateNoWindow被设置为其非默认值。 UseShellExecute默认情况下为true,我们将其设置为false,因为当UseShellExecute为true时,CreateNoWindow会忽略其自身的值(CreateNoWindow),并创建一个新窗口(即使CreateNoWindow设置为true)。默认情况下,CreateNoWindow为false,我们将其设置为true。如果要使用CreateNoWindow,则唯一不多余且可行的用例将是UseShellExecute = false和CreateNoWindow = true。 (如果UseShellExecute = true(默认值),则应使用隐藏的进程窗口样式。)还要记住,这些内容适用于cmd.exe,但不能适用于calc.exe之类的其他内容

Note here- UseShellExecute and CreateNoWindow are being set to their non default values. UseShellExecute is by default true, and we set it to false because when UseShellExecute is true then CreateNoWindow ignores the value of itself(CreateNoWindow), and a new window is created(even if CreateNoWindow is set to true). CreateNoWindow is by default false, and we set it to true. If you are going to use CreateNoWindow, then the only non superfluous and working use case would be UseShellExecute=false and CreateNoWindow=true. (If UseShellExecute=true(default) then you should use process window style hidden.. ) Also bear in mind these things work for cmd.exe but fail for some other things like calc.exe

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
Process.Start(psi);

这篇关于C#我无法使CreateNoWindow正常工作-甚至msdn.com示例都没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆