如何处理Windows Services中的致命错误? [英] How Do I Handle Fatal Errors In Windows Services?

查看:83
本文介绍了如何处理Windows Services中的致命错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了一些有关如何编写Windows服务的示例,但是在服务中的错误处理方面我没有发现太多.如果发生严重到足以应关闭服务的错误,最好的处理方法是什么?我看到的唯一代码是加载ServiceControlManager对象,通过检查进程ID来查找自身并向自身发送停止命令.有更好的方法吗?

Nathan Holt

I''ve found several examples of how to write a Windows service, but I haven''t found much on error handling in services. What is the best way to handle it if an error occurs that''s severe enough that the service should be shut down? The only code I''ve seen loaded a ServiceControlManager object, looked itself up by checking process IDs and sent itself a stop command. Is there a better way?

Nathan Holt

推荐答案

Windows服务不是什么大问题.它仅由Windows而不是用户运行.所以你可能想做的是
*在尝试启动进程或服务的地方使用try-catch块,并记录异常,就像抛出异常一样.
*您可以创建一个全局错误处理程序,记录您的服务所做的一切.

您可以在这里找到一些详细的信息;

异常管理规则 [异常处理文章 [ C#的全局异常处理程序代码 [
Well windows services is not a big deal. It is just running by windows instead of an user. So what you might want to do is;
*Use a try-catch block where you start your process or your service and log the exception as if throws it.
*You can create a global error handler which logs everything your service does.

Some detailed info you can find here;

Exception Management Rules[^]

and some more articles about it;

Exception Handling Articles[^]

And a global exception handler;

Global Exception Handler Code for C#[^] ( this is an extra, since you havent identified a language, I thought it might be useful..)

Good luck.


谢谢你的回答,但没有解释我想要的.如果该服务正在运行的后台线程退出,Windows仍可以认为该服务正在运行.如果整个过程都退出会发生吗?

我通过搜索"application.threadexception Windows服务"确实找到了更多信息.
http://http://stackoverflow.com/questions/2014562/whats-the-difference-between-application-threadexception-and-appdomain-currentdo> http中找到了Application.ThreadException与AppDomain.UnhandledException的解释://stackoverflow.com/questions/2014562/whats-the-difference-between-application-threadexception-and-appdomain-currentdo [ ^ ]

现在,我希望只调用Environment.Exit是在出现致命错误时结束服务的合理方法.
Thanks for your answer, but it didn''t explain what I wanted. If the background thread that the service is running on exits, Windows can still think the service is running just fine. Does it happen if the whole process exits?

I did find more information by googling "application.threadexception windows service" There''s an interesting discussion at http://objectmix.com/dotnet/110512-handling-unhandled-exception-net-service.html[^]. I also found explanations of Application.ThreadException vs AppDomain.UnhandledException at http://stackoverflow.com/questions/2014562/whats-the-difference-between-application-threadexception-and-appdomain-currentdo[^]

For now, I''m going to hope that just calling Environment.Exit is a reasonable way to end the service if there''s a fatal error.


这篇关于如何处理Windows Services中的致命错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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