简体   繁体   English

Windows服务无法启动

[英]Windows Service failed to start

This is my main: 这是我的主要:

static class Program
{
    static void Main()
    {
        //Debugger.Launch();          
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new Service1() 
        };

        ServiceBase.Run(ServicesToRun);
    }
}

And this is my Service1() code: 这是我的Service1()代码:

 public partial class Service1 : ServiceBase
{
    public Service1()
    {
        Thread messageThread = new Thread(new ThreadStart(Messaggi.Check));
        messageThread.Start();

        bool checkGruppoIndirizzi = true;

        for (; ; )
        {
            SediOperative.Check();

            Autisti.Check();

            AutistiVeicoli.Check();

            StatiVega.Check();

            int res = Ordini.Check();
            if (res == 0) AssegnazioniVega.Check();

            Thread.Sleep(10000);
        }
    }

    protected override void OnStart(string[] args)
    {
    }

    protected override void OnStop()
    {
    }
}

First thing is I don't know if launching two threads in that way is a good thing to do, but the real problem is the program run fine inside Visual Studio but after installation (I've created a setup project using InstallShield) I try to start my service from the windows service panel and I get: 第一件事是我不知道以这种方式启动两个线程是否是一件好事,但是真正的问题是程序在Visual Studio中运行良好,但是安装后(我已经使用InstallShield创建了安装项目),我尝试从Windows服务面板启动我的服务,我得到:

Error 1053: The service did not respond to the start or control request in a timely fashion

The problem you have is that your service will be started sucessfully after the susyem has called the Start method and it has sucessfully returned. 您遇到的问题是,在susyem调用了Start方法并成功返回之后,服务将成功启动。 Given that you have an infinite loop in the constructor, the system is saying to itself something like "Can't even create the this let alone call start. I'm giving up.' 鉴于构造函数中存在无限循环,系统会自言自语,例如“甚至无法创建this更不用说调用start。我正在放弃。”

Your code should be refactored along these lines: 您的代码应按照以下方式进行重构:

 public partial class Service1 : ServiceBase
{
    public Service1()
    {
    }
    private Thread messageThread;
    private Thread otherThread;

    private bool stopNow;

    protected override void OnStart(string[] args)
    {
        this.stopNow = false;
        this.messageThread = new Thread(new ThreadStart(Messaggi.Check));
        this.messageThread.Start();

        this.otherThread = new Thread(new ThreadStart(this.StartOtherThread));
        this.otherThread.Start();

    }

    private void StartOtherThread()
    {
        bool checkGruppoIndirizzi = true;

        while (this.stopNow == false)
        {
            SediOperative.Check();

            Autisti.Check();

            AutistiVeicoli.Check();

            StatiVega.Check();

            int res = Ordini.Check();
            if (res == 0) AssegnazioniVega.Check();

            for (int 1 = 0; i < 10; i++)
            {
                if (this.stopNow)
                {
                    break;
                }
                Thread.Sleep(1000);
            }
        }
    }
    }
    protected override void OnStop()
    {
            this.stopNow = true;
        this.messageThread.Join(1000);
        this.otherThread.Join(1000);
    }
}

And yes, starting stuff on Threads is exactly the way to do it! 是的,在Threads上开始的东西正是这样做的方法! You'll have to have some way of stopping them in the Stop() method. 您必须在Stop()方法中有一些停止它们的方法。 (The code above is air code so don't trust it.) for the 'otherThread' I've got it checking a bool and exiting when the bool is set. (上面的代码是空代码,因此请不要信任它。)对于“ otherThread”,我已经检查了bool并在设置bool时退出。 the thread.Join is just a tidy-up which isn't strictly necessary, but is good housekeeping I think. 参加只是一个整洁的工作,严格来说并不是必须的,但是我认为这是很好的内务处理。

Cheers - 干杯-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM