Create Process and Wait

P

Peter

I am using CreateProcessWithLogonW to run an external application, the
problem I am having is that the C# program does not wait until the external
program finishes running before continuing. The external program is a
console application and it does not interact with users, how can I make my
C# program wait until the program initiated by CreateProcessWithLogonW API
completes running before continuing?

here's my API code.

bool ret = CreateProcessWithLogonW(_logonName, _domain,
_password,
LOGON_WITH_PROFILE, null, sb,
NORMAL_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT,
IntPtr.Zero, "c:\\",
ref startInfo, out processInfo);


Peter
 
J

Julie

Peter said:
I am using CreateProcessWithLogonW to run an external application, the
problem I am having is that the C# program does not wait until the external
program finishes running before continuing. The external program is a
console application and it does not interact with users, how can I make my
C# program wait until the program initiated by CreateProcessWithLogonW API
completes running before continuing?

here's my API code.

bool ret = CreateProcessWithLogonW(_logonName, _domain,
_password,
LOGON_WITH_PROFILE, null, sb,
NORMAL_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT,
IntPtr.Zero, "c:\\",
ref startInfo, out processInfo);

Peter

Whatever the equivalent for WaitForSingleObject is in .Net.

You call WFSO on the processInfo process handle. When that becomes signaled,
the process has terminated.
 
G

Guest

Hi Peter,

Thank you for posting in the community!

Based on my understanding, you use CreateProcessWithLogonW to create a new
process through P/invoke. You want to be notified when that new process
exits.

======================================
Because the new created process has almost no relationship with your
process, there is not an easy way to access the information of the new
created process.
Interprocess access in Win32 need much support of the operating system.

And in .Net, there is no way to get what you want, because .Net does not
expose function for interprocess operation. If you can I suggest you use
.Net Process class to replace CreateProcessWithLogonW API.
For Process class, it expose an Exited event, which you can be notified
when you created process exited.

If you still can not use Process class, the only workaround I can think of
is kept enumerating all the processes in the system to query if the new
created process exited.

This is not a good solution, and it has a poor performance.

For How to enumerate process in Win32, please refer to:
http://support.microsoft.com/default.aspx?scid=kb;en-us;175030

=====================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
S

Stephen Martin

You need to wait on the process handle. This handle becomes signalled when
the process exits. You can PInvoke to WaitForSingleObject, passing in
processInfo.hProcess as the handle to wait on. Or you can subclass the
System.Threading.WaitHandle class and assign processInfo.hProcesss to its
Handle property - then use the normal WaitOne method.

Also, remember to close processInfo.hProcess and processInfo.hThread when
you no longer need them (calling Dispose or Close on a WaitHnadle subclass
will also do this).
 
G

Guest

Hi Stephen,

Oh, I think my original reply has some error. Yes, we can P/invoke
WaitForSingleObject to get this done.

Thanks!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Hi Peter,

Is your problem resolved?

If you still have any concern, please feel free to feedback, we will help
you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
P

Peter

Sorry!, I have been working on another project, I will try the solution in a
few days.
 
G

Guest

Hi Peter,

Thanks very much for your feedback.

Ok, once you have any concern on this issue, please feel free to feedback.
I will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
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

Similar Threads


Top