在服务启动时处理异常 [英] Handle exception on service startup

查看:122
本文介绍了在服务启动时处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一系列Windows服务。如果在启动期间抛出错误,我希望它们失败(在 OnStart()方法中)。我假设只是在 OnStart()中抛出一个错误,可以这样做,但我发现,而是开始,并向我发送一条消息,指出服务已经开始,但是没有活动,这是正确的吗? (释义)。如何处理错误,以致实际上无法启动服务?

解决方案

如果您正在运行.NET 2.0或更高版本,您可以使用 ServiceBase.Stop 停止服务从OnStart。否则从新线程呼叫Stop。



ref [devnewsgroups]( http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic50404.aspx



(新闻组消失)


I'm writing a series of Windows services. I want them to fail if errors are thrown during startup (in OnStart() method). I had assumed that merely throwing an error in OnStart() would do this, but I'm finding that instead it "Starts" and presents me with a message stating "The service has started, but is inactive. Is this correct?" (Paraphrase). How do I handle the error so it actually fails to start the service?

解决方案

if you are running .NET 2.0 or higher, you can use ServiceBase.Stop to stop the service from OnStart. Otherwise call Stop from a new thread.

ref [devnewsgroups] (http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic50404.aspx)

(news group gone)

这篇关于在服务启动时处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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