开发者

Windows service start, stop, debug problems

I have a service that I don't know the runtime of, I guess around 7 seconds. For some reason the service stops working after the first run and I can't debug it. It keeps saying "starting" on the services manager and I can't find it on the attach pr开发者_Python百科ocess window.

When I try to stop it, the stop button appears for only a second. Even if I press it I get an error saying "windows could not stop the service 'Splive on local computer. the service did not return an error. this could be an internal windows error or internal service error."

What would be the best way to handle this issue?

static void Main(string[] args)
    {
        ServiceBase.Run(new Program());
        ServiceController service = new ServiceController();
        service.ServiceName = "SpLive";
        service.Start();
        //Sp objSportingbet = new Sp();
        //objSportingbet.getListingsFromSp();
    }
    public Program()
    {
        this.ServiceName = "SpLive";
    }
    protected override void OnStart(string[] args)
    {
        base.OnStart(args);
        objSportingbet.getListingsFromSp();
        timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
        timer1.Interval = 7000;
        timer1.Enabled = true;
        timer1.Start();
    }
    protected override void OnStop()
    {
        base.OnStop();
        timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
        timer1.Interval = 7000;
        timer1.Enabled = false;
        timer1.Start();
    }
    private void timer1_Elapsed(object sender, EventArgs e)
    {
        ServiceController service = new ServiceController();
        service.ServiceName = "Sp";
        if (service.Status == ServiceControllerStatus.Stopped)
        {
            service.Start();
        }
        if (service.Status == ServiceControllerStatus.Running)
        {
            service.Stop();
        }
        timer1.Stop();
    }

    private void InitializeComponent()
    {
        // 
        // Program
        // 
        this.CanPauseAndContinue = true;
        this.CanShutdown = true;

    }


Configure the service to start under the debugger: http://support.microsoft.com/kb/824344 Note the section "Configure a service to start with the WinDbg debugger attached"

Addition (now have code in question):

static void Main(string[] args)
{
   ServiceBase.Run(new Program());
   ServiceController service = new ServiceController();
   service.ServiceName = "SpLive";
   service.Start();

ServiceBase.Run(instance) will not return until the service is shutdown, so you are running the service, and then after it has shutdown asking the SCM to run the service… this will only lead to confusion.

This, plus having a timer to try and reverse the state (started <-> stopped) of the service makes me think you need to think about the underlying process model of a Windows service:

When there is only one service implemented by the exe:

  1. The service is started (at system startup, from a user request, ...): the SCM runs the registered command line

  2. Main runs, tell the SCM (via ServiceBase.Run) what service this is. This must match the registration used in step 1.

  3. The instance passed to ServiceBase.Run has its OnStart called. The service should start activities it will perform and then return (ie. asynchronous operations, new threads and the thread pool are OK; continuing on the thread that calls OnStart is not).

  4. When the signal to shutdown arrives (from whatever source) OnStop is called. This should trigger stopping all the activities that OnStart started (or started since) and wait for them to stop and then return.

The only reason for a service to stop itself would be if something else (eg. its own management API) triggered it, but it would be better to use the SCM from the UI.


Ideally you would want to debug your service's OnStart method to see what's going on. And it is possible:

protected override void OnStart(string[] args)
{
    #if DEBUG
    Debugger.Launch();
    #endif
    ...
}

This works even when service is not flagged as desktop interactive.


The OnStart and OnStop handlers have a fixed time limit for being handled. I don't know how stopping works (whether it waits for a thread to be done..), but for OnStart, in your case (I know its an old thread..) I'd move all of the application code into a timer callback and set the timer in the OnStart function. I set mine to about 1 minute. The OnStart will exit immediately, which satisfies the service managers requirements. But now you have a thread that will start in about a minute, which allows you time to attach your process to the debugger. Obviously set a break point on the first instruction in the OnStart timer callback.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜