C# “服务就是报告”;服务报告了无效的当前状态0。”;

C# “服务就是报告”;服务报告了无效的当前状态0。”;,c#,loops,windows-services,C#,Loops,Windows Services,请您帮助我找出我的服务在事件日志中报告以下错误消息的原因: [service name]服务报告了无效的当前状态0 以下是我的主要功能: static class Program { /// <summary> /// The main entry point for the application. /// </summary> static void Main() { // Setup Ninject

请您帮助我找出我的服务在事件日志中报告以下错误消息的原因:

[service name]服务报告了无效的当前状态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
    }
}
当我在VisualStudio中调试该服务时,它工作得非常好,但一旦我部署它,它似乎会非常缓慢地处理项目,并报告该错误消息

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

我没有包括这方面的代码,因为我担心这个错误可能是服务设置不正确的症状——不是线程中执行的代码


我想知道我是否有什么设置错误?

解决方案在这里,我相信:。我遇到了完全相同的问题,找到了这个页面和我链接到的页面。在ServiceStatus结构的互操作声明中将“long”更改为“uint”。

可能重复的
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);
    }
}