让应用程序运行,即使未处理的异常发生 [英] Keep an Application Running even if an unhandled Exception occurs

查看:122
本文介绍了让应用程序运行,即使未处理的异常发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么?
我正在开发一个需要保持全天候运行控制台应用程序的不管是什么
有没有什么办法可以从得到炸毁一些未处理的异常发生在部分停止多线程应用程序线程某处?

What ?
I am developing a Console Application that needs to keep running 24/7 No Matter What
Is there any way to stop a Multi-Threaded Application from getting blown up by some unhandled exception happening in "some thread somewhere" ?

为什么?
请从给像你应该管理所有的异常,这不应该发生等我有我的理由不要教训:我们在测试部署,我们需要保持这种运行状态,记录异常,并重新启动所有线程。如果什么意外的发生并导致未处理的异常被抛出,它需要被检测,并呼吁重新启动所有的线程(原子性是不可能的,因为由于该层设计)

Why ?
Please refrain from giving lessons like "you should manage all your exceptions", "this should never happen" etc. I have my reasons : We are in test deployment and we need to keep this running, log exceptions, and restart all threads again. If anything unplanned happens and causes an unhandled exception to be thrown, it needs to be detected and some method called to restart all threads(atomicity is impossible due due the tier design)

这是说,我知道它可能不能够从内,因为和UnhandledException(我已经实现)。如果它烧断

This being said, I am aware it might no be possible to restart an application from "within" if it has blown because of and UnhandledException (which I already implemented).

到目前为止,我已经使用Quartz.net的FileScan工作和一个标志文件来检测这样的东西并从向外重新启动应用程序。但是,这听起来很哈克给我。我想找到的东西更清洁,更少的快速和肮脏的。

So far, I have used Quartz.net's FileScan Job and a Flag File to detect stuff like that and restart the application from outwards. But this sounds hacky to me. I would like to find something cleaner and less quick and dirty.

DownVoting /狙击警告:我知道这也许是不可能的是'。请勇于创新/有益的而不是突然的批评,并认为这更多的是一个开放式问题

推荐答案

您需要附加一个事件处理程序UnhandledException事件对当前的AppDomain:

You need to attach an event handler to UnhandledException event on the Current AppDomain:

AppDomain.CurrentDomain.UnhandledException + = UnhandledExceptionHandler

在处理函数中,你将不得不以某种方式保存足够的状态(到一个文件,数据库等)的重新启动的应用程序一起传递给新的线程。然后实例化你的控制台应用程序通过调用一个新的实例 System.Diagnostics.Process.Start(MyConsoleApp.exe)

Inside the handler, you will have to somehow save enough state (to a file, database, etc.) for the restarted application to pass along to the new threads. Then instantiate a new instance of your Console application with a call to System.Diagnostics.Process.Start("MyConsoleApp.exe").

要非常小心,引进逻辑,避免死机/重启/死机/重启的连续循环。

Be very careful to introduce logic to avoid a continuous loop of crash/restart/crash/restart.

这篇关于让应用程序运行,即使未处理的异常发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆