System.Diagnostics.ProcessStartInfo

M

Mark

Hi...

I was trying to spawn a process and not have it create a new window. No
matter how I set CreateNoWindow, it always creates a new window when it
spawns. Does this property simply not work? Should I be using windowstyle =
hidden instead?

ProcessStartInfo pi = new ProcessStartInfo();
pi.FileName =
Path.GetFullPath(Environment.ExpandEnvironmentVariables(appPath));
pi.Arguments = "-i";
pi.CreateNoWindow = true;
Process proc = Process.Start(pi);
proc.WaitForExit();

Thanks
Mark
 
P

Peter Duniho

Hi...

I was trying to spawn a process and not have it create a new window. No
matter how I set CreateNoWindow, it always creates a new window when it
spawns. Does this property simply not work? Should I be using
windowstyle =
hidden instead?

Probably. I'm not entirely sure about the CreateNoWindow property, but if
I recall correctly, that's for console or shell applications. There's
nothing you can do via the process starting mechanism that would suppress
a window that the process itself is creating explicitly. You can only
suppress windows that Windows might create on behalf of the process.

Even setting the window style to hidden might not perfectly address it.
But it's worth a try.

Pete
 
J

Jeffrey Tan[MSFT]

Hi Mark,

Yes, Pete is right. CreateNoWindow property internally encapsulates the
CREATE_NO_WINDOW flag passing to the Win32 CreateProcess API. From the
document below, you will see that it is merely used to hide the console
application window. This flag is ignored if the application is not a
console application:
http://msdn2.microsoft.com/en-us/library/ms684863(VS.85).aspx

If one GUI application is intended to be used to perform the background
processing work, it should accept certain command line arguments to run
without any GUI. Letting the end user to hide its GUI window is not a good
practise.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mark

Hi Jeffrey, Peter...

The thing that made me curious is that the app I'm launching is a console
app, but it still spawns a separate window.

To be specific, I'm writing a little application that helps with installing
an ASP.Net website, so my .net program is spawning aspnet_regiis.exe to make
sure the right version of .net is the default. That's a console app.

But when I use the Process.Start() code I posted, it ignores the
CreateNoWindow option and spawns a separate window. My .net app is also a
console app.

It seems that the flag just doesn't do anything at all.

Thanks
Mark
 
J

Jeffrey Tan[MSFT]

Hi Mark,

Thanks for your feedback.

Based on my original experience, CreateNoWindow property should work. I
suspect that the application that you started with Process.Start() is
hidden, but this application may have created a new console process whose
console is visible.

I would recommend you to download Process Explorer from the link below and
use the "Window Find" tool in the Process Explorer to examine what process
is this visible console window belong to:
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mark

Hi Jeffrey...

As mentioned before, the app my .net code is launching is
\windows\microsoft.net\framework\v2.0.50727\aspnet_regiis.exe

The problem with the test you mention is that it doesn't run very long; it
would be near impossible to get process explorer to latch onto it.

Interestingly when you run aspnet_regiis.exe from a command window, it
doesn't launch another window when it executes; it uses the cmd window. I
don't know why it feels compelled to launch another window when spawned by
Process.Start();

Thanks
Mark
 
J

Jeffrey Tan[MSFT]

Hi Mark,

Thanks for your feedback.

I created a simple Winform project with your code snippet and I can
reproduce this problem now. Further research shows that this is a
documentation issue of MSDN. The effect of CreateNoWindow property actually
depends on another property -- UseShellExecute, while MSDN did not mention
this.

Actually, there are two logics embeded in the Process.Start. By default,
the UseShellExecute property is set to true, which means that
Process.Start() will use the shell logic to start a process. That is it
will internally call ShellExecuteExW win32 function to perform the task
instead of using CreateProcess Win32 API. So, no CREATE_NO_WINDOW flag is
passed to CreateProcess function. So, to hide the console window, we should
set UseShellExecute property to false with setting CreateNoWindow property
to true.

Also, I find that the CLR developer Mike Stall has reported this issue on
his blog below:
"How to start a console app in a new window, the parent's window, or no
window"
http://blogs.msdn.com/jmstall/archive/2006/09/28/CreateNoWindow.aspx

You may try to set UseShellExecute property to false and CreateNoWindow
property to true to see if it works for you.(It works on my side):

string strExe =
@"c:\windows\microsoft.net\framework\v2.0.50727\aspnet_regiis.exe";
ProcessStartInfo pi = new ProcessStartInfo();
pi.FileName = strExe;
pi.Arguments = "-i";
pi.UseShellExecute = false;
pi.CreateNoWindow = true;
Process proc = Process.Start(pi);
proc.WaitForExit();

Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffrey Tan[MSFT]

Additionally, if you do not set UseShellExecute property, and set a
breakpoint on ShellExecuteExW API, we will get the following stack trace,
which proves my statement:

0:000> kL
ChildEBP RetAddr
0012ef64 053b2a32 shell32!ShellExecuteExW
0012ef80 7a669f49 CLRStub[StubLinkStub]@53b2a32
0012efe8 7a668e6c
System_ni!System.Diagnostics.ShellExecuteHelper.ShellExecuteOnSTAThread()+0x
81
0012efe8 7a667df4
System_ni!System.Diagnostics.Process.StartWithShellExecuteEx(System.Diagnost
ics.ProcessStartInfo)+0x2d8
023f21f4 7a6691f7 System_ni!System.Diagnostics.Process.Start()+0x80
023f21f4 0237039a
System_ni!System.Diagnostics.Process.Start(System.Diagnostics.ProcessStartIn
fo)+0x33
023f21f4 7b060bdb
WinFormTest!WinFormTest.Form1.button1_Click(System.Object,
System.EventArgs)+0x72
0012f0a8 7b1292e9
System_Windows_Forms_ni!System.Windows.Forms.Control.OnClick(System.EventArg
s)+0x57
0012f0a8 7b1293ef
System_Windows_Forms_ni!System.Windows.Forms.Button.OnClick(System.EventArgs
)+0x49
0012f0a8 7b0f68c6
System_Windows_Forms_ni!System.Windows.Forms.Button.OnMouseUp(System.Windows
.Forms.MouseEventArgs)+0xc3
0012f0a8 7b074fef
System_Windows_Forms_ni!System.Windows.Forms.Control.WmMouseUp(System.Window
s.Forms.Message ByRef, System.Windows.Forms.MouseButtons, Int32)+0xf2
0012f114 7b0841ce
System_Windows_Forms_ni!System.Windows.Forms.Control.WndProc(System.Windows.
Forms.Message ByRef)+0x56f
0012f150 7b0840eb
System_Windows_Forms_ni!System.Windows.Forms.ButtonBase.WndProc(System.Windo
ws.Forms.Message ByRef)+0xce
0012f1b0 7b07d24d
System_Windows_Forms_ni!System.Windows.Forms.Button.WndProc(System.Windows.F
orms.Message ByRef)+0x2b
0012f1b0 7b07d226
System_Windows_Forms_ni!System.Windows.Forms.Control+ControlNativeWindow.OnM
essage(System.Windows.Forms.Message ByRef)+0xd
0012f1b0 7b07d035
System_Windows_Forms_ni!System.Windows.Forms.Control+ControlNativeWindow.Wnd
Proc(System.Windows.Forms.Message ByRef)+0xd6
0012f1b0 00392164
System_Windows_Forms_ni!System.Windows.Forms.NativeWindow.Callback(IntPtr,
Int32, IntPtr, IntPtr)+0x75

Just for your information.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Mark,

Have you reviewed my reply to you? Does it make sense to you? If you still
need any help, please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mark

Hi Jeffrey...

Sorry I let this drop; I got pulled into another project.

I read your advice and the blog link you posted, and it all makes sense.
Thank you for taking the time to research it so thoroughly.

Mark
 
J

Jeffrey Tan[MSFT]

Hi Mark,

Glad to see it can help you. If you need further help, please feel free to
post, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top