Reputation: 41
I'm using the Win32 API function CreateProcessAsUser()
to run a single-instance application that talks to an API running as a Windows service. I'm using this implementation to create and open the single-instance app and pass arguments to it.
I'm having two issues:
the handle of the new process is different than the handle of the running console application, and so WaitForSingleObject()
returns immediately.
even if I get the handle of the running console app, by using the Process.GetProcessesByName()
method, WaitForSingleObject()
is stuck waiting but the console app must be running.
A long time ago, I found a method to wait until the single-instance app writes a response to the console, but I can't remember the method's name.
So, I need to wait until the running console app writes something to the console, and then read the console to get that response.
By the way, I'm using C# in both applications, the API and the single-instance app.
Upvotes: 0
Views: 136
Reputation: 596277
the handle of the new process is different than the handle of the running console application, and so
WaitForSingleObject()
returns immediately.
This is normal behavior for a single-instance application. If the application is already running when you start a new process for it, that process will detect the existing instance and so terminate itself (possibly after sending your new command-line arguments to it). You are waiting on the handle of the new process, not the existing process. When the new process terminates, the handle you are waiting on will be signaled, satisfying the wait.
even if I get the handle of the running console app, by using the
Process.GetProcessesByName()
method,WaitForSingleObject()
is stuck waiting but the console app must be running.
Makes sense. You are waiting on a handle to a running process. The handle will not be signaled until the process terminates.
A long time ago, I found a method to wait until the single-instance app writes a response to the console, but I can't remember the method's name.
You can't capture console output for an existing process, only for a new process that you spawn with CreateProcess...()
. See Creating a Child Process with Redirected Input and Output on MSDN.
Upvotes: 0