.NET - WindowStyle = hidden vs. CreateNoWindow = true?

C#.NetProcessProcessstartinfo

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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, he can't get the program activated anymore and can only kill it with Task Manager.

Solution 3 - C#

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;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGaborView Question on Stackoverflow
Solution 1 - C#LizView Answer on Stackoverflow
Solution 2 - C#Hans PassantView Answer on Stackoverflow
Solution 3 - C#SwDevMan81View Answer on Stackoverflow