My service won't stop???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Steele

    #1

    My service won't stop???

    Okay, I assume I'm missing something obvious here. I have created a simple
    service in C# that on starting spawns a thread to do some processing. The
    service can be stopped with a "net stop" command of course, but under some
    circumstances the service will decide to terminate itself. My OnStart looks
    something like this:

    protected override void OnStart(string[] args)
    {
    serviceThread = new Thread(new ThreadStart(Ser viceThreadStart ));
    serviceThread .Start();
    }

    So this is simple enough. The problem is that if I use "net stop ..." to
    stop my service everything works fine. My thread detects the stop request
    and shuts itself down, and the service itself then terminates. However, if
    the service thread encounters some condition where it decides to shut itself
    down, the service keeps on running. I assume I have to call some method to
    tell the service control manager that the service is ending but I cannot
    find what method I need to user. I even tried inserting an
    Application.Exi t() call in the service shutdown code and that didn't do the
    trick. What do I need to call to get the service to terminate?


  • Peter Steele

    #2
    Re: My service won't stop???

    I just added the code

    ServiceControll er sc = new ServiceControll er(this.Service Name);
    sc.Stop();

    in my thread's shutdown logic and that did the trick. I don't think this is
    the best way to do it though....
    [color=blue]
    > Okay, I assume I'm missing something obvious here. I have created a simple
    > service in C# that on starting spawns a thread to do some processing. The
    > service can be stopped with a "net stop" command of course, but under some
    > circumstances the service will decide to terminate itself. My OnStart
    > looks something like this:
    >
    > protected override void OnStart(string[] args)
    > {
    > serviceThread = new Thread(new ThreadStart(Ser viceThreadStart ));
    > serviceThread .Start();
    > }
    >
    > So this is simple enough. The problem is that if I use "net stop ..." to
    > stop my service everything works fine. My thread detects the stop request
    > and shuts itself down, and the service itself then terminates. However, if
    > the service thread encounters some condition where it decides to shut
    > itself down, the service keeps on running. I assume I have to call some
    > method to tell the service control manager that the service is ending but
    > I cannot find what method I need to user. I even tried inserting an
    > Application.Exi t() call in the service shutdown code and that didn't do
    > the trick. What do I need to call to get the service to terminate?
    >
    >[/color]


    Comment

    Working...