C#Windows服务 - 处理异常的启动 [英] C# Windows Service - Handle Exception on Startup

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

问题描述

我写了一系列的Windows服务。我想,如果在启动过程中抛出错误(的OnStart()方法)他们失败。我原以为只是抛出一个错误,在的OnStart()将做到这一点,但我发现,而是开头和presents我一个消息,说明该服务已启动,但无效。这是正确的吗? (释义)。如何做到这一点,实际上无法启动服务,我处理错误?

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?

推荐答案

如果您运行的是.NET 2.0或更高版本,可以使用的 ServiceBase.Stop 以阻止的OnStart服务。否则,从一个新的线程中调用停止。

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.

<打击>文献[devnewsgroups](的http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic50404.aspx)

(新闻集团走了)

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

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