locked
error stop a Windows service RRS feed

  • Question

  •  

    Hello everyone,


    Here is my OnStop method of Windows service application. When click stop, the error message is, "Error: 1067 The process terminated unexpectedly".

    I have tested if remove the Join method, the service can stop gracefully. Does it mean Join method is not allowed to be called in OnStop method of a Windows service? Any ideas?

    [Code]
    protected override void OnStop()
    {
        MyWorkerThread.Join();
        this.ExitCode = 0;
    }
    [/Code]



    thanks in advance,
    George

    Friday, May 30, 2008 7:49 AM

Answers

  • Dear  George2,

      As given in the code you are trying to join a running thread in the OnStop method.

    protected override void OnStop()
    {
        MyWorkerThread.Join();
        this.ExitCode = 0;
    }

    First of all When we try to stop a Windows Service, it has to be stopped immediately. On one side we want to stop the Windows Service and on the other we are trying to delay the "stop" mechanism by using the thread.Join method. I guess here when you Stop the service, the OnStop method is invoked and a ThreadAbort execption is thrown in your thread so as to stop the thread instead of joining it. Thus you are receiving  the service terminated unexpectedly.
      I would recommend you to keep the thread activities outside the OnStop event.
     

    Hope this helps you!

    Sandeep Aparajit | http://sandeep-aparajit.blogspot.com | Mark usefull posts as Answer/Helpful.
    • Marked as answer by jack 321 Tuesday, June 3, 2008 6:20 AM
    Saturday, May 31, 2008 6:52 PM

All replies

  • Dear  George2,

      As given in the code you are trying to join a running thread in the OnStop method.

    protected override void OnStop()
    {
        MyWorkerThread.Join();
        this.ExitCode = 0;
    }

    First of all When we try to stop a Windows Service, it has to be stopped immediately. On one side we want to stop the Windows Service and on the other we are trying to delay the "stop" mechanism by using the thread.Join method. I guess here when you Stop the service, the OnStop method is invoked and a ThreadAbort execption is thrown in your thread so as to stop the thread instead of joining it. Thus you are receiving  the service terminated unexpectedly.
      I would recommend you to keep the thread activities outside the OnStop event.
     

    Hope this helps you!

    Sandeep Aparajit | http://sandeep-aparajit.blogspot.com | Mark usefull posts as Answer/Helpful.
    • Marked as answer by jack 321 Tuesday, June 3, 2008 6:20 AM
    Saturday, May 31, 2008 6:52 PM
  • You should signal all your threads to exit, perhaps with a ManualResetEvent or a flag that they check oftern. Then you are correct in calling Join to wait for them to exit. The SCM will never kill your service process, although the UI might give up and say that your service is not responding.

    The problem must be something in your service is crashing or calling Environment.Exit with a nonzero return code when you ask it to stop.
    Friday, November 14, 2008 6:14 PM