ProcessStartInfo ProcessWindowStyle.Minimized has no effect?

S

Steve Brecher

Framework 1.1:

ProcessStartInfo psi = new ProcessStartInfo(@".\appdir\aConsoleApp.exe");
psi.WorkingDirectory = @".\appdir";
psi.WindowStyle = ProcessWindowStyle.Minimized;
psi.UseShellExecute = false;
Process.Start(psi);

It works fine except that the started app's console window is normal -- not
minimized. Why?
 
T

Tian Min Huang

Hello Steve,

Thanks for your post. I reviewed your code carefully and now I'd like to
share the following information with you:

It is the following line that causes the console window not being minimized:
psi.UseShellExecute = false;

That indicates to start a process directly from the executable file. As you
know, a console window does not create a window itself, I believe, that may
be the reason why ProcessWindowStyle.Minimized does not take any effect. To
work around this problem, I suggest you to set UseShellExecute to true to
use the operating system shell to start a console process.
psi.UseShellExecute = true;

Note: UseShellExecute defaults to true.

If you need to redirect input, output and error streams, you may have to
set UseShellExecute. If so, you can call Win32 API ShowWindow to minimize
the console window after it is shown. Please refer to the following code
snippet:

//-----------------code snippet------------------------
using System.Runtime.InteropServices;
...
[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd,Int32 nCmdShow);

private Int32 SW_MINIMIZE = 2;

...

psi.UseShellExecute = false;
Process.Start(psi);

Process [] myProcess = Process.GetProcessesByName("aConsoleApp");
IntPtr hwnd = myProcess[0].MainWindowHandle;
ShowWindow(hwnd, SW_MINIMIZE);
//-------------------end of-----------------------------

ProcessStartInfo.UseShellExecute Property
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdiagnosticsprocessstartinfoclassuseshellexecutetopic.asp

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
S

Steve Brecher

Tian Min Huang said:
It is the following line that causes the console window not being minimized:
psi.UseShellExecute = false;

Right. Removing that, and thus accepting the default value of true, solves
the problem. I had thought I needed "false" because I had been confused by
the part of the documentation on UseShellExecute that discusses its effect
on the WorkingDirectory property of ProcessStartInfo.

(I also changed the FileName property value to just the name of the
executable and not a path.)
That indicates to start a process directly from the executable file. As you
know, a console window does not create a window itself, I believe, that may
be the reason why ProcessWindowStyle.Minimized does not take any effect. To
work around this problem, I suggest you to set UseShellExecute to true to
use the operating system shell to start a console process.
psi.UseShellExecute = true;

Note: UseShellExecute defaults to true.

If you need to redirect input, output and error streams, you may have to
set UseShellExecute. If so, you can call Win32 API ShowWindow to minimize
the console window after it is shown. Please refer to the following code
snippet:

//-----------------code snippet------------------------
using System.Runtime.InteropServices;
..
[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd,Int32 nCmdShow);

private Int32 SW_MINIMIZE = 2;

..

psi.UseShellExecute = false;
Process.Start(psi);

Process [] myProcess = Process.GetProcessesByName("aConsoleApp");
IntPtr hwnd = myProcess[0].MainWindowHandle;
ShowWindow(hwnd, SW_MINIMIZE);
//-------------------end of-----------------------------

ProcessStartInfo.UseShellExecute Property
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdiagnosticsprocessstartinfoclassuseshellexecutetopic.asp
 

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