473,587 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
Nov 22 '05 #1
1 2575
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....
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?

Nov 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
363
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 of course, but under some circumstances the service will decide to terminate itself. My OnStart looks something like this: protected override...
3
610
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 service to shutdown gracefully, and it takes a long time to save all data while exiting. Thanks for any help, Jan
2
5272
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 stop code .Abort()s the threads and .Stop()s the listener. One one of my computers, the service runs like, well...a service. It starts and stops...
1
1506
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 NIC this is true, so I try .Stop().
0
1106
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 account and I am now using impersonation. The following is the warning I am receiving in the System Event Log. The server was unable to logon the...
4
1508
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 ASP.net application or open an existing application, start the application (F5) in the IDE, close the IE window that the application started up, now you...
2
1157
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 TcpListener Dim tcpclient As TcpClient Try
1
1209
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
2643
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 software should behave when it is sent a stop-service, but what would send it a stop????? On shutting down the computer, I could understand, but that...
0
1315
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: ================================= Private m_thTCP As Thread Private m_listener As TcpListener
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.