Home All Groups Group Topic Archive Search About

Stopping Windows Service with Thread

Author
21 Dec 2006 7:32 PM
tshad
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

Author
21 Dec 2006 8:04 PM
Mudhead
theThread.IsBackground = True

Show quoteHide quote
"tshad" <tscheider***@ftsolutions.com> wrote in message
news:%23o12FbTJHHA.4376@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
>
Author
21 Dec 2006 10:22 PM
Stephany Young
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.


Show quoteHide quote
"Mudhead" <noth***@yourhouse.com> wrote in message
news:urYKzsTJHHA.1280@TK2MSFTNGP04.phx.gbl...
> theThread.IsBackground = True
>
> "tshad" <tscheider***@ftsolutions.com> wrote in message
> news:%23o12FbTJHHA.4376@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
>>
>
>