tags:

views:

251

answers:

2

I try to pragrammatically run applications such as notepad.exe and Calc.exe with the following codes. I can see the application is activated in Process Exprorer but the application UI does not appear on the screen. I got this info inside the process p - "Process must exit before requested information can be determined" and the exitcode are 1200, 5084, etc. What is wrong? Thanks!

Codes -

ProcessStartInfo pInfo = new ProcessStartInfo(@"C:\Windows\system32\notepad.exe");
pInfo.UseShellExecute = false;
pInfo.CreateNoWindow = false;
pInfo.WindowStyle = ProcessWindowStyle.Normal;
Process p = Process.Start(pInfo);
p.EnableRaisingEvents = true;
int exitCode = p.Id;
p.WaitForExit();
p.Close();
+1  A: 

Whoops edit try this:

Process p = Process.Start(pInfo);
p.EnableRaisingEvents = true;
p.WaitForExit();
int exitCode = p.ExitCode;
p.Close();
Will
The code runs fine; there seems to be a misunderstanding between the `Id` and `ExitCode` properties of the `Process` class.
Austin Salonen
@austin it must be. I switched the two and tested and I got the error he asked about. Updated.
Will
Thanks for the exitCode. That is not my biggest concern. The biggest concern is that the notepad does not show up in desktop so I can type something in it. I do see the notepad.exe is running in Process Explorer. My application is hosted in Windows Service.
Don
@Don windows services don't run in the user space and cannot show UI. A Service runs under a user (usually some built in account such as Network Service), so if you imagine two users logged onto your machine at the same time it would be like you opening up Notepad and the other user seeing it open as by magic on his desktop. In short, what you're trying to do cannot reasonably be done. It would be better for you to ditch this and ask a question in which you state what you would like to do (I want to write an app that...) and get feedback on if it is possible or different ways to make it.
Will
@Will Excelent. That is what I look for. Thanks!
Don
+1  A: 

There is a misunderstanding between Id and ExitCode. Your code assumes the Process ID is the Exit Code and it is not (your "exit codes" are process IDs).

Try this code:

ProcessStartInfo pInfo = new ProcessStartInfo(@"C:\Windows\system32\notepad.exe");
pInfo.UseShellExecute = false;
pInfo.CreateNoWindow = false;
pInfo.WindowStyle = ProcessWindowStyle.Normal;
Process p = Process.Start(pInfo);
p.EnableRaisingEvents = true;
p.WaitForExit();
int exitCode = p.ExitCode;
p.Close();

MSDN Process.ExitCode
MSDN Process.Id

Austin Salonen