Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to stop Threads!

Status
Not open for further replies.

hallian92

Programmer
Jul 30, 2002
8
US
Hi,
I have a main program in which I create five more threads. When these threads are created I show a Dialogbox with a ProgressBar for each of the threads status. Means if I have five threads I show five ProgressBars one for each thread that shoes the status of the threads completion. I also a Button that when clicked stops all the threads and closes the dialog box. But my problem is that when I click a Button the Dialogbox closes but the Threads still keep on running at the back. How can I kill or stop all the threads when a Button is clicked. I really apprecitae for any help. I tried to use Thread.stop() it does work but it's deprecated. I read the Docs. that say to use a variable and that when chages exits the run method. But I don't know how to incorporate this as I have five or more threads. I have a common run method and in that I am calling some other local methods too. I don't know how and where to set the boolean variable. Any small example will help alot or any tips.
Thanks
 
The Thread method join has the same effect to my knowledge but it would be better to have run test with a while or for loop. Call the respective thread's stopThread() method when you want it to quit, after all run only executes as far as I know so to keep it going it must have a loop.

Public class myThread extends Thread {
private int percentage;
private boolean canRun = true;

public void stopThread() {
canRun = false;
}
public void run() {
while(canRun) {
//Do Stuff
this.yield(); //I think that's right, may be
//Thread.yield()
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top