469,951 Members | 2,744 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

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(ServiceThreadStart));
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.Exit() 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?
Nov 22 '05 #1
1 2403
I just added the code

ServiceController sc = new ServiceController(this.ServiceName);
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....
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(ServiceThreadStart));
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.Exit() 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?

Nov 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Peter Steele | last post: by
1 post views Thread by Franziskus Scharpff | last post: by
reply views Thread by Brent Burkart | last post: by
2 posts views Thread by LS | last post: by
3 posts views Thread by =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post: by
reply views Thread by =?Utf-8?B?anAybXNmdA==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.