problem with teminate thread ... Thanks~~

  • Thread starter Thread starter Ivan
  • Start date Start date
I

Ivan

Hi All,

my question is how to teminate a running thread...
it sounds simple but in fact i 'm having problem with it

firstly, i create a new thread (refer to follow example code) and start it
on form load
once the form button on clicked... i try terminate the thread... but an
exception was catched...

example code as follow..
Public Class CounterIncrement()
Public lngCounter As Long = 0

Pubilc Sub Increment()
Do
lngCounter += 1
Loop Until intCounter > 100000
End Sub
End Class

Public Class TestForm : Inherits System.Windows.Form

Private objThread As Thread()

Private Sub form1_load() handles form1.load
Dim objCounter As New CounterIncrement()

objThread = New Thread(AddressOf objCounter.Increment)
objThread.Start

Do
Application.DoEvents
Loop Until Not objThread.IsAlive
end sub

Private Sub button1_Click() handles button1.click
objThread.Abort <==== this won't terminate the thread... but make it
state as AbortRequested....?? why???
End Sub

End Class
 
* "Ivan said:
my question is how to teminate a running thread...
it sounds simple but in fact i 'm having problem with it

firstly, i create a new thread (refer to follow example code) and start it
on form load
once the form button on clicked... i try terminate the thread... but an
exception was catched...

example code as follow..
Public Class CounterIncrement()
Public lngCounter As Long = 0

Pubilc Sub Increment()
Do
lngCounter += 1
Loop Until intCounter > 100000
End Sub
End Class

Public Class TestForm : Inherits System.Windows.Form

Private objThread As Thread()

Private Sub form1_load() handles form1.load
Dim objCounter As New CounterIncrement()

objThread = New Thread(AddressOf objCounter.Increment)
objThread.Start

Do
Application.DoEvents
Loop Until Not objThread.IsAlive
end sub

Private Sub button1_Click() handles button1.click
objThread.Abort <==== this won't terminate the thread... but make it
state as AbortRequested....?? why???
End Sub

Read the documentation about 'Thread.Abort'.
 
Usually the Abort method should be used as a last resort, a cleaner way,
such as a flag should be used.

--
HTH,
-- Tom Spink, Über Geek

Woe be the day VBC.EXE says, "OrElse what?"

Please respond to the newsgroup,
so all can benefit
 
Hi Ivan,

In addition to the answer from Herfried because in the documentation there
is as far as I know is not a "why", just that it is.

The thread is ended on its own after a while that there was no action
anymore.
However it is not real processing either.

So you should in my opinion not try to get the information if the thread is
running from the thread itself however from your program logic.

Just my thought,

Cor
 
Back
Top