0

I have a Windows application of which I need multiple instances running, with different command line parameters. The application is quite unstable and tends to crash every 48 hours or so.

Since manual checking for failure and restarting in case of one isn't what I love to do I want to write a "manager program" for this. It would launch the program (all its instances) and then watch them. In case a process crashes it would be restarted.

In Linux I could achieve this with fork()s and pids, but this obviously is not available in Windows. So, should I try to implement a CreateProcess version or is there a better way?

2
  • Does your application need to be interactive or can it be run as a daemon?
    – MC ND
    Jan 9, 2014 at 10:29
  • Yes it can, I will be the only one to use it.
    – user4520
    Jan 9, 2014 at 11:11

2 Answers 2

1

When you call CreateProcess, you are returned a handle to the new process in the hProcess member of the process information struct that you pass to CreateProcess. You can use this handle to detect when the process terminates.

For instance, you can create another thread and call WaitForSingleObject(hProcess) and block until the process terminates. Then you can decide whether or not to restart it.

Or your could call GetExitCodeProcess(hProcess, &exitcode) and test exitcode. If it has the value STILL_ACTIVE then your process has not terminated. This approach based on GetExitCodeProcess necessitates polling.

5
  • I've tested the first approach and it looks quite straightforward, however it seems that for Windows a crashed process doesn't actually terminate until the user clicks the "Close Program" button in that error dialog box (the unstable app is a GUI program), so I can't catch such events this way.
    – user4520
    Jan 9, 2014 at 22:24
  • OK. You will need to decide by what criteria you decree a process to be "crashed". Jan 9, 2014 at 22:27
  • The basic test I did was I've created a "master" process that launched a "child" process, which slept for 3 seconds then tried to divide by 0, of course crashing, however this wasn't recognized by the parent as "termination" until I've clicked this "End Program" button. That's more or less what happens with the GUI program I need to monitor.
    – user4520
    Jan 9, 2014 at 22:29
  • Like I said, you need to find a way to detect a process in whatever the bad state is. There will not be one single way to do that. Jan 9, 2014 at 22:30
  • Alright, for people having the same problem: if you use this approach and disable windows error dialogs as described here: stackoverflow.com/questions/396369/… it should work nicely.
    – user4520
    Jan 9, 2014 at 22:53
1

If it can be run as a daemon, the simplest way to ensure it keep running is Non-Sucking Service Manager.

It will allow to run as win32 service applications not designed as services. It will monitor and restart if necessary. And the source code is included, if any customization is needed.

All you need to do is define each of your instances as a service, with the required parameters, at it will do the rest.

If you have some kind of security police limitation and can't use third party tools, then coding will be necessary. The answer from David Heffernan gives you the appropiate direction.

Or it can be done in batch, vbs or js without need of anything out of the system. WMI Win32_Process class should allow you to handle it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.