473,763 Members | 9,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stopping Windows Service with Thread

I have a Service that starts a thread that never ends.

When I stop the service from the Service Applet, does it kill the thread or
do I need to do it myself?

If it isn't killed, what happens when I start the service again.

*************** *************** *************** *****
Protected Overrides Sub OnStart(ByVal args() As String)
Dim theThread As Thread
theThread = New Thread(AddressO f StartThreads)
theThread.Start ()
End Sub

Protected Overrides Sub OnStop()
End Sub

Private Sub StartThreads()
Dim oCredit1 As New CreditProcessor

....
End Sub
*************** *************** *************** *********

Thanks,

Tom
Dec 21 '06 #1
2 1946
theThread.IsBac kground = True

"tshad" <ts**********@f tsolutions.comw rote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>I have a Service that starts a thread that never ends.

When I stop the service from the Service Applet, does it kill the thread
or do I need to do it myself?

If it isn't killed, what happens when I start the service again.

*************** *************** *************** *****
Protected Overrides Sub OnStart(ByVal args() As String)
Dim theThread As Thread
theThread = New Thread(AddressO f StartThreads)
theThread.Start ()
End Sub

Protected Overrides Sub OnStop()
End Sub

Private Sub StartThreads()
Dim oCredit1 As New CreditProcessor

...
End Sub
*************** *************** *************** *********

Thanks,

Tom

Dec 21 '06 #2
There is a gotcha with the IsBackground property of a Thread object is that
when the process is terminated, the thread in question is killed dead. It
does not get the opportunity to execute any 'clean-up' logic and can be
compared to killing a process from Task Manager.

For critical threads in services I use something like this:

Private m_thread As Thread = Nothing
Private m_threadstop As Boolean = False

Protected Overrides Sub OnStart(ByVal args() As String)

m_thread = New Thread(AddressO f StartThreads)

m_thread.Start( )

End Sub

Protected Overrides Sub OnStop()

m_stopthread = True

m_thread.Join()

End Sub

Private Sub StartThreads()

' Any required start-up logic goes here

While Not m_threadStopped

' the main logic the thread goes here

If WeNeedToExitEar ly() Then Exit While

Thread.Sleep(1)
End While

' Any required tidy-up logic goes here

End Sub

The each iteration of the control loop in thread needs to be short enough
(time wise) so that the service can respond to the Stop request from the
Service Control Manager in a timely fashion i.e. within about 20 seconds.
"Mudhead" <no*****@yourho use.comwrote in message
news:ur******** ******@TK2MSFTN GP04.phx.gbl...
theThread.IsBac kground = True

"tshad" <ts**********@f tsolutions.comw rote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>>I have a Service that starts a thread that never ends.

When I stop the service from the Service Applet, does it kill the thread
or do I need to do it myself?

If it isn't killed, what happens when I start the service again.

************** *************** *************** ******
Protected Overrides Sub OnStart(ByVal args() As String)
Dim theThread As Thread
theThread = New Thread(AddressO f StartThreads)
theThread.Start ()
End Sub

Protected Overrides Sub OnStop()
End Sub

Private Sub StartThreads()
Dim oCredit1 As New CreditProcessor

...
End Sub
************** *************** *************** **********

Thanks,

Tom


Dec 21 '06 #3

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

Similar topics

5
2617
by: CG | last post by:
Hi I have developed a Windows Service When I try to start the Service it tells me that it cannot start as there may not be any work to do When I comment out below line of code in my OnStart event.... objEventLog.EnableRaisingEvents = Tru ....It starts fin I have the same issue when stopping the service because of below line of code in my OnStop even
6
5094
by: DK | last post by:
i was able to successfully create a windows service using py2exe. it polls a website periodically and logs it to a file. this is done using a function that does an infinite loop with periodic "sleeps". my question is... what's the best way to stop this service gracefully? when try to stop it from the services applet from control panel, it takes forever and then gives me an error.
4
15895
by: Keith | last post by:
I'm in the same boat as the fellow who posted this message back in August: Title : Windows Service, How does one make a service "fail" properly? Author : Ross Bennett Group : microsoft.public.dotnet.languages.csharp URL :...
3
2077
by: karl | last post by:
I have a windows service that creates a monitor thread which in turn creates 4 worker threads. Each thread is based upon a derived class (HL7Listener) of the TcpListener class. When running this service on my workstation I have no problems whatsoever. I then deployed this to another server and cannot open any of the sockets. Troubleshooting this issue I've discovered that the 4 threads do successfully enter the HL7Listener constructor....
7
10077
by: Gene | last post by:
I have a Windows Service (VB.NET) that runs 24/7. It queries a Web service 5 to 10 times per hour. About 2 or 3 times a month, it fails. The log indicates that it sends the request to the Web service but there is no return from the service. Stopping and restarting the Windows service cures the problem. Our desire is to cure the problem with appropriate error handling but failing that, is there an easy way to automatically stop and...
2
2259
by: matteo | last post by:
Hi everyboby, i wrote a c# service that every XXX minute launch a working thread on timer events, something like this: private void timer_Elapsed ( object status ) { // Worker thread System.Threading.ThreadPool.QueueUserWorkItem ( new System.Threading.WaitCallback( _timer_Elapsed ) );
6
8118
by: D | last post by:
I have a simple file server utility that I wish to configure as a Windows service - using the examples of the Python Win32 book, I configured a class for the service, along with the main class functions __init__, SvcStop, and SvcDoRun (which contains my server code). After registering the service, I am able to start it with no problems. However, it never stops correctly (net stop returns "service could not be stopped") and service is left...
10
7542
by: archana | last post by:
Hi all, I am having one windows service which is updating to database. On 'Onstop i want to wait till current updation complete. How will i do this? Because if i write some lengthy code on onstop it fails to stop and status remain 'stopping' Later i can't do anything on that service even i can't uninstall that service.
1
2534
by: raghudr | last post by:
Hi all, I am displaying a splash screen for which i have created a thread.Since my whole project is launched by windows service and that service will start automatically at the start of the PC and sometimes when i start or restart the PC i have observed that even though the long process is completed splash
0
9564
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
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
3
2794
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.