Reputation: 4476
I have a Windows service(C#), that sprawns few child native processes (C++).
I'd like to gently kill those processes once in a while. (gently = let the procs to finalize its work before going down).
I tried to use the SetConsoleCtrlHandler() routine to register the child procs to console events and to call the CloseMainWindow() from the C# code to raise the "close console" events.
This didn't work for me. Mainly because the child processes are not console applications.
Does anyone knows what other methods can be used in order to fulfill this requirement?
Upvotes: 1
Views: 1002
Reputation: 68074
Have a look at the technique of using QueueUserAPC and exceptions I described here. You might find it useful.
Upvotes: 0
Reputation: 6307
Post a message to the child processes. This can be a message of your own choosing, so you can pick up on the message in the child processes, and then do what needs to be done, see WM_USER.
Upvotes: 2
Reputation: 21898
Kernel event objects come to mind: your "manager" raises a named event. Your child processes should check the state of this event at least once in a while (or have a thread that continuously checks it).
Upvotes: 4
Reputation: 37850
C# has a native way to access the services running. You will need the "System.ServiceProcess" namespace, and the code should look kind of like this:
ServiceController[] services = ServiceController.GetServices();
for(int i = 0; i < services.Length; i++)
{
if (services[i].DisplayName == "COM+ System Application")
{
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
}
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
}
}
}
}
Upvotes: -1