当我开始一个新流程时,如果我使用
,会有什么不同WindowStyle = hidden
或
CreateNoWindow = true
ProcessStartInfo
类的
属性
答案 0 :(得分:74)
正如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);
答案 1 :(得分:17)
CreateNoWindow仅适用于控制台模式应用程序,它不会创建控制台窗口。
WindowStyle仅适用于本机Windows GUI应用程序。这是一个传递给这个程序的WinMain() entry point的提示。第四个参数,nCmdShow,告诉它如何显示其主窗口。这是在桌面快捷方式中显示为“运行”设置的相同提示。请注意,“隐藏”不是那里的选项,很少有正确设计的Windows程序尊重该请求。由于这会扼杀用户,他无法再激活程序,只能使用任务管理器将其终止。
答案 2 :(得分:12)
使用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;