By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,417 Members | 904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,417 IT Pros & Developers. It's quick & easy.

Thread Killing

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Cor
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
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

Nov 20 '05 #2

P: n/a
"Michael" <IN******@aol.com> schrieb
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?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3

P: n/a
"Armin Zingler" <az*******@freenet.de> wrote in message
news:OR**************@TK2MSFTNGP11.phx.gbl...
"Michael" <IN******@aol.com> schrieb
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?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


The code I posed is it. That is all I am doing with threads.
Nov 20 '05 #4

P: n/a
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/de...ingExample.asp

http://msdn.microsoft.com/msdnmag/is...t/default.aspx

http://msdn.microsoft.com/library/de...enersample.asp

HTH,
Trev.

"Michael" <IN******@aol.com> wrote in message
news:tU******************@twister.tampabay.rr.com. .. "Armin Zingler" <az*******@freenet.de> wrote in message
news:OR**************@TK2MSFTNGP11.phx.gbl...
"Michael" <IN******@aol.com> schrieb
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?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


The code I posed is it. That is all I am doing with threads.

Nov 20 '05 #5

P: n/a
"Michael" <IN******@aol.com> schrieb
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?


The code I posed is it. That is all I am doing with threads.


You don't create a thread, consequently you can not kill a thread.
--
Armin
Nov 20 '05 #6

P: n/a
* "Michael" <IN******@aol.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #7

P: n/a
* "Michael" <IN******@aol.com> scripsit:
System.Threading.Thread.Sleep(200)


This will block the whole UI thread, it doesn't start an other thread.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #8

P: n/a
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

"Trev Hunter" <hu*********@hotmail.com> wrote in message
news:uh**************@TK2MSFTNGP10.phx.gbl...
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/de...ingExample.asp
http://msdn.microsoft.com/msdnmag/is...t/default.aspx

http://msdn.microsoft.com/library/de...enersample.asp
HTH,
Trev.

"Michael" <IN******@aol.com> wrote in message
news:tU******************@twister.tampabay.rr.com. ..
"Armin Zingler" <az*******@freenet.de> wrote in message
news:OR**************@TK2MSFTNGP11.phx.gbl...
"Michael" <IN******@aol.com> schrieb
> 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?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


The code I posed is it. That is all I am doing with threads.


Nov 20 '05 #9

P: n/a
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
* "Michael" <IN******@aol.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>


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
Nov 20 '05 #10

P: n/a
Cor
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
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

Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.