一些服务自动停止,如果他们没有被其他服务使用 [英] Some Services stop automatically if they are not in use by other services

查看:1294
本文介绍了一些服务自动停止,如果他们没有被其他服务使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误的一些服务自动停止,如果他们不使用其他服务试图启动一个窗口服务。

Error "SOME SERVICES STOP AUTOMATICALLY IF THEY ARE NOT IN USE BY OTHER SERVICES" while trying to start a windows service.

我不使用Windows服务配置文件,并使用静态属性服务 - 它工作得很好。

I have a service that does not use the windows service config file and uses static properties - it works fine

现在,我使用app.config文件,重建我的设置项目+服务项目。现在我安装的服务,然后尝试启动服务 - 我得到以下错误:

Now, i make use of app.config file and rebuild my setup project + the service project. Now i install the service and then try to start the service - i get the following error:

一些服务自动停止,如果他们不使用其他服务。

SOME SERVICES STOP AUTOMATICALLY IF THEY ARE NOT IN USE BY OTHER SERVICES

服务登录作为本地系统。

Service logs on as local system.

任何投入是值得欢迎的,请!谢谢。

Any input is welcome please! Thanks.

推荐答案

这是一般的结果两件事情之一 - 无论是(​​a)您的OnStart() 方法抛出异常或(b)在的OnStart()方法不蹬掉一个线程做的工作。

This is generally the result of one of two things - either (a) your OnStart() method is throwing an exception or (b) the OnStart() method is not kicking off a thread to do work.

如果问题是(一),则明显的解决方案是调试服务,以确定什么错误。至少,把周围的的OnStart()法的内容的try-catch 块并记录错误当发生异常时系统事件日志。然后,你可以看到在Windows事件查看器的详细信息。

If the problem is (a), then the obvious solution is to debug the service to identify what is going wrong. At a minimum, put a try-catch block around the contents of the OnStart() method and log an error to the system event log when an exception occurs. Then you can see the details in the Windows Event Viewer.

如果问题是(B),那么你需要创建一个线程,实际上做一些事情。线程需要是一个前台线程(而不是在后台线程),以防止该服务从关闭。一个典型的的OnStart()方法是这样的:

If the problem is (b), then you need to create a thread that actually does something. The thread needs to be a foreground thread (as opposed to a background thread) to prevent the service from shutting down. A typical OnStart() method looks like this:

private System.Threading.Thread _thread;

protected override void OnStart(string[] args)
{
    try
    {
        // Uncomment this line to debug...
        //System.Diagnostics.Debugger.Break();

        // Create the thread object that will do the service's work.
        _thread = new System.Threading.Thread(DoWork);

        // Start the thread.
        _thread.Start();

        // Log an event to indicate successful start.
        EventLog.WriteEntry("Successful start.", EventLogEntryType.Information);
    }
    catch (Exception ex)
    {
        // Log the exception.
        EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
    }
}

private void DoWork()
{
    // Do the service work here...
}

这篇关于一些服务自动停止,如果他们没有被其他服务使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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