Thread Killing

  • Thread starter Thread starter Michael
  • Start date Start date
M

Michael

How do I kill the thread of one button with the clicking of another button?

ie
Private Sub btnEnglish_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEnglish.Click

System.Threading.Thread.Sleep(200)

'Do This

End Sub



Private Sub btnSpanish_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSpanish.Click

'Kill btnEnghish's Thread

'Do That

End Sub




Thanks,
Michael
 
Hi,

A little bit pseudo

Private MyThread as system.threading.thread

fucntion start
MyThread = New System.Threading.Thread(AddressMyTreadStart)
MyThread.Priority = Threading.ThreadPriority.Normal
MyThread.Start()

function kill
MyThread.Abort

I hope this helps?

Cor
 
Michael said:
How do I kill the thread of one button with the clicking of another
button?

ie
Private Sub btnEnglish_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnEnglish.Click

System.Threading.Thread.Sleep(200)

'Do This

End Sub



Private Sub btnSpanish_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnSpanish.Click

'Kill btnEnghish's Thread

'Do That

End Sub

Where do you create and start a thread?
 
Michael,
The code I posed is it. That is all I am doing with threads.

In that case, both your button event handlers run on the same thread
(because they both are created by the parent form). If btnEnglish_Click
blocks this thread, then you won't be able to click on btnSpanish (unless
there is a call to Application.DoEvents() in btnEnglish_Click).

If you want to interrupt btnEnglish's processing when clicking on
btnSpanish, then you'll have to use some kind of flag in your form class.

I wouldn't recommend doing it like this. If you need to use threads, perhaps
you couls start one when clicking on a button (be careful you don't start it
twice if the user double-clicks on the button). My advice is to learn as
much as possible about threads before doing this - inclucing the pitfalls of
using them with windows forms.

Here's some links that may help:

http://msdn.microsoft.com/library/d...n-us/vbcn7/html/vaconFreeThreadingExample.asp

http://msdn.microsoft.com/msdnmag/issues/01/07/vbnet/default.aspx

http://msdn.microsoft.com/library/d...dowsformsmultithreadedtciiplistenersample.asp

HTH,


Trev.
 
* "Michael said:
How do I kill the thread of one button with the clicking of another button?

ie
Private Sub btnEnglish_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEnglish.Click

System.Threading.Thread.Sleep(200)

'Do This

End Sub



Private Sub btnSpanish_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSpanish.Click

'Kill btnEnghish's Thread

'Do That

'btnEnglish' doesn't have its own thread.
 
Thanks for the reply,

I do have many Application.DoEvents() in the button event that I want to
stop. They are to update the text box.

Michael
 
Herfried K. Wagner said:
'btnEnglish' doesn't have its own thread.

Thanks guys,

Let me post more details, maybe that will help.

btnEnglish's click event has text update, then thread sleep, then doevents
then repeats that cycle (but not in a loop) That goes on for a while.

If I click btnSpanish, it puts text in the same text box but does NOT have
thread sleeps and doevents.

If I start btnEnglish, then click btnSpanish, btnSpanish does what it is
supposed to do, but then btnEnglish continues and overwrites btnSpanish's
text. I want to stop btnEnglish from continuing on if btnSpanish gets
clicked mid-btnEnglish's tasks.

Does that make it more clear?

Michael
 
Hi Michael,

All processes (subs) are always completly completed, but I think I
understand what you want to do. I think that you need something like this
dirty typed here as pseudo

\\\
Private swEngl as boolean
Private swFirstTime as boolean
///
\\\\
Sub ButtonEnglishpushed
If Not swEngl then
swEngl = True
StartProces
end if
end Sub
Sub ButtonSpanishpushed
If swEng then
swEngl = False
StartProces
end if
end sub
StartProces
if not swfirsttime
dostopcurrentproces
end if
swfirstime = false
if swEngl then
startProcesEnglish
else
startProcesSpanish
end sub
///

I hope this helps?

Cor
 
Back
Top