473,507 Members | 2,377 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(AddressOf 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 1930
theThread.IsBackground = True

"tshad" <ts**********@ftsolutions.comwrote in message
news:%2****************@TK2MSFTNGP03.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(AddressOf 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(AddressOf 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 WeNeedToExitEarly() 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*****@yourhouse.comwrote in message
news:ur**************@TK2MSFTNGP04.phx.gbl...
theThread.IsBackground = True

"tshad" <ts**********@ftsolutions.comwrote in message
news:%2****************@TK2MSFTNGP03.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(AddressOf 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
2587
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...
6
5069
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...
4
15857
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 :...
3
2064
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...
7
10036
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...
2
2235
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...
6
8017
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...
10
7501
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...
1
2508
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...
0
7223
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,...
0
7319
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,...
0
7376
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...
0
5623
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,...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
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 ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
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...

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.