服务报告“服务已报告无效的当前状态0”。

时间:2015-06-08 17:57:45

标签: c# loops windows-services

请您帮我解决一下为什么我的服务在事件日志中报告以下错误消息:

  

[服务名称]服务报告了无效的当前状态0。

这是我的主要功能:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        // Setup Ninject
        var kernel = ConfigureNinject();

        #if DEBUG

        var service = kernel.Get<UpdateTakerStatus>();
        service.onDebug();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

        #else

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        {
            kernel.Get<UpdateTakerStatus>()
        };
        ServiceBase.Run(ServicesToRun);

        #endif
    }
}

...这里是我的UpdateTakerStatus类,我的逻辑在一个新线程中被调用:

public partial class UpdateTakerStatus : ServiceBase
{
    private Thread WorkerThread;
    private AutoResetEvent StopRequest = new AutoResetEvent(false);
    private ServiceStatus serviceStatus = new ServiceStatus();

    public ITakerStatusService TakerStatusService { get; set; }

    [DllImport("advapi32.dll", SetLastError = true)]
    private static extern bool SetServiceStatus(IntPtr handle, ref ServiceStatus serviceStatus);

    public UpdateTakerStatus(ITakerStatusService takerStatusService)
    {
        InitializeComponent();

        this.TakerStatusService = takerStatusService;
    }

    public void onDebug()
    {
        this.OnStart(null);
    }

    protected override void OnStart(string[] args)
    {
        // Update the service state to Start Pending.
        serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING;
        serviceStatus.dwWaitHint = 100000;
        SetServiceStatus(this.ServiceHandle, ref serviceStatus);

        this.WorkerThread = new Thread(UpdateTakers);
        this.WorkerThread.Start();

        // Update the service state to Running.
        serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
        SetServiceStatus(this.ServiceHandle, ref serviceStatus);
    }

    private void UpdateTakers()
    {
        while (true)
        {
            #if (!DEBUG)
            // Run this code until the service is stopped
            if (StopRequest.WaitOne())
            {
                return;
            }
            #endif

            this.TakerStatusService.CalculateFinalResultsForTakers();
        }
    }

    protected override void OnStop()
    {
        serviceStatus.dwCurrentState = ServiceState.SERVICE_STOP_PENDING;
        SetServiceStatus(this.ServiceHandle, ref serviceStatus);

        this.StopRequest.Set();
        this.WorkerThread.Join();

        serviceStatus.dwCurrentState = ServiceState.SERVICE_STOPPED;
        SetServiceStatus(this.ServiceHandle, ref serviceStatus);
    }
}

当我在visual studio中调试它时,该服务完全正常,但是一旦我部署它,它似乎很慢地处理项目并报告错误消息。

然而,它似乎是处理项目,表明代码设置正在执行有希望的事情。处理在循环内的服务方法调用this.TakerStatusService.CalculateFinalResultsForTakers();中执行。

我没有包含此代码,因为我担心此错误可能是服务通常设置错误的症状 - 而不是线程中执行的代码?

我想知道我的设置是否错误?

1 个答案:

答案 0 :(得分:3)

解决方案在这里,我相信:Service Causes SCM Error "reported an invalid current state 0"。我遇到了完全相同的问题,发现了这个页面和我链接的那个页面。在ServiceStatus结构的Interop声明中将“long”更改为“uint”。