473,323 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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 2556
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Steele | last post by:
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...
3
by: Jan Waga | last post by:
Hello, Is there some way to control SCM timeout while starting or stopping a C# windows service? I know I can work on a different thread and quickly return from OnStart(), but I'd like my...
2
by: 0to60 | last post by:
I have a windows service that when started creates two threads: one thread that runs a TcpListener waiting for TcpClients to connect, and another thread that reads from the resulting sockets. My...
1
by: Franziskus Scharpff | last post by:
Hi all, I need to disable a NetworkInterface, so I thought to work with ServiceProcess. When I do getDevices() I get a list of all DeviceServices which I then check for "CanStop". For the...
0
by: Brent Burkart | last post by:
I no longer am able to debug my asp.net application. I have made numerous changes that may have affected this however, I am having difficulties pinning the source down. I did change the aspnet user...
4
by: Chad | last post by:
I rebuilt my pc (format and reinstall) running XP and Visual Studio 2003. My problem is that the IDE won't stop the run session when I close the web application. Now when I create a new...
2
by: LS | last post by:
Hello, I have a windowsforms application which spins a new thread. Code of the thread ----------------------------------------------------------------- Sub DoListen() Dim listener As...
1
by: baroque Chou | last post by:
I add validation control to my page, but it seems it dosn't work using input. is it true?
3
by: =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post by:
HELP!!!!! I created a service that cleans out files in a directory. The problem is it works for a while, then something (windows perhaps?) sends it a stop-service cmd. I figure that my...
0
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I have a windows application that does not stop running whenever the application exits. Could someone fill me in on what I am doing wrong? Here is the relevant code:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.