且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重新启动未处理异常的应用程序

更新时间:2022-10-22 19:28:51

我以前在Windows中完成了这个操作,方法是从另一个程序通过win32 CreateProcess 调用。然后其他程序等待监视进程退出,并再次调用其 CreateProcess()。您可以通过在进程'上执行 WaitForSingleObject 等待进程退出句柄,您可以从 CreateProcess()调用中返回值之一。



您当然会想要以某种方式进行编程,使监控过程自动关闭,其子进程将关闭。


Is it possible to have a program restart automatically if it crashes?

Something like:

  1. An unhandled exception is thrown.
  2. Release all resources allocated by process.
  3. Start over and call main.

I would like this behavior for a server application I'm working on. If clients miss use the server it can get a std::bac_alloc exception, in which case I would like the server to simply restart instead of crashing and shutting down, thus avoiding manual startup.

I've done this before in Windows by running said program from another program via a win32 CreateProcess call. The other program then waits on the "monitored" process to exit, and calls its CreateProcess() again if it does. You wait for a process to exit by performing a WaitForSingleObject on the process' handle, which you get as one of the return values from your CreateProcess() call.

You will of course want to program in some way to make the monitoring process shut itself and its child process down.