自动启动Windows服务的方法 [英] Method to start windows service automatically

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

问题描述

大家早上好,

我有一项服务,我想知道是否有任何方法可以自动启动该服务.例如:我的服务每天24小时运行.即使将CanStop方法设置为false,由于电量不足,终端重新启动或其他任何问题,有时服务有时也会掉落.

到目前为止,我检查的唯一方法是设置一种不时重新启动服务的方法,但是那样对我不利,因为它将极大地影响Windows服务的性能.

有人可以帮我吗?

感谢所有人

对不起我的英语.我不是很好还是很漂亮

Good morning everyone,

I have a service,and I was wondering if there is any method to do the service automatically start.For example: my service run 24 hours a day. Even with the method CanStop set to false, the service can sometimes occur fall, either for lack of energy,the terminal being rebooted, or any other problem.

The only way I checked so far,would be setting a method to restart the service from time to time, but isnt good for me that way because it will greatly influence the performance of the windows service.

Could someonegive me a hand?

Thanks to all

sry about my english. isnst good or pretty

推荐答案

我希望您知道服务可以在系统启动时自动启动,这是服务安装的标准选项之一可以随时使用服务控制器或服务小程序进行更改:
I hope you know that the services can be started automatically on system start-up, this is one of the standard options of the service installation which can be changed at any time using the Service Controller or services applet:
%windir%\system32\services.msc


因此,如果重新启动运行该服务的主机,该服务将自动重新启动.

与其他干扰服务的问题怎么办?我同意您的意见:定期不定期重新启动服务的想法将无法正常工作.

好吧,您确实需要使您的服务能够容错.请遵循以下原则:

在单独的线程中执行每个逻辑上不同的活动.确保您没有使用随机或不可预测的线程数.最好的方法是在一开始就创建固定数量的线程,并使它们处于工作状态或等待状态,等待某些阻塞调用(例如在联网的情况下)或对线程同步原语的调用.在这种状态下,线程将消耗零的CPU时间,直到被某个线程同步事件唤醒为止.

通过将所有异常包装在try-catch-finally块中,在每个线程的最顶部捕获所有异常.此外,设计一个无限"的外部执行循环,在这里您可以利用最后一次机会捕获任何异常并尝试重新启动.从示意图上看,它应该像这样:


So, if the host running the service is rebooted, the service will restart automatically.

What to do with other problems disrupting the service? I agree with you: the idea of regular restarting of a service from time to time would not work properly.

Well, you will really need to make your service fail-tolerant. Use the following principles:

Execute every logically distinct activity in a separate thread. Make sure you are not working with random or unpredictable number of threads. The best approach is creating a fixed number of threads in the very beginning and keeping them either working or in a wait state, waiting at some blocking call (like in case of networking) or a call to thread synchronization primitives. Is such state, the thread consumes zero CPU time until waken up by some thread synchronization event.

Catch all exceptions at the very top of each thread by wrapping it in the try-catch-finally block. Besides, devise an "infinite" outer execution loop where you can use a last chance to catch any exception and try to restart. Schematically, it should look like this:

void ServiceThreadBody() { //something passed to the constructor of System.Threading.Thread.Thread
    while(!exitCondition) {
        try {
            MainThreadCycle();
        } catch (MySpecificException e) {
            ProcessSpecificException(e);
        } catch (MyOtherSpecificException e) {
            ProcessAnotherSpecificException(e);
        //... some other exception handling specific particular exception types you can foresee 
        } catch { //all other exceptions; not generally recommended but important in this case
            ProcessUnexpectedException();
        } finally {
            SomeUniversalCleanUp();
        } //exception
    } //main restart loop
} //ServiceThreadBody

void MainServiceCycle() {
    while (keepServicingCondition) {/* do the main job... */}
} //MainServiceCycle



处理异常时,需要执行必要的清理.您需要做的一件事是使用类System.Diagnostics.EventLog将异常信息记录到系统日志中,请参阅:
http://msdn.microsoft.com/en-us/library/system.diagnostics. eventlog.aspx [ ^ ].

在执行服务的每个线程中执行类似的操作.

有关容错的某些特定想法,例如网络服务,请参阅我过去的回答:
来自同一端口号的多个客户端 [如何在文件夹下创建事件日志 [ ^ ],
将MsBuild OutPut即时发送到TextBox Windows应用程序中 [ ^ ].

请查看我过去有关异常处理的一些想法的答案:
当我运行应用程序时,例外是捕获了如何处理此问题? [扔. .then ... rethrowing [ ^ ],
在类库(dll)中处理异常 [ ^ ].

—SA



When you handle an exception, you need to perform necessary clean up. One thing you would need to do is to log the exception information to the system log using the class System.Diagnostics.EventLog, please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Do something like that in each thread performing the service.

For some particular idea on fault tolerance, on the example of network service, please see my past answer:
Multple clients from same port Number[^].

Please see my past answers on advanced use of logging:
How to create event log under a folder[^],
MsBuild OutPut to the TextBox on the fly in Windows Application[^].

Please see my past answers on some ideas on exception handling:
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^],
Handling exceptions in class library (dll)[^].

—SA


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

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