Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   How do I start a thread by pressing a button? Simple hey? (http://www.velocityreviews.com/forums/t135789-how-do-i-start-a-thread-by-pressing-a-button-simple-hey.html)

C-man 08-02-2004 10:18 PM

How do I start a thread by pressing a button? Simple hey?
 
Basically I have this program that will traverse a list of files in a
directory and will rename each file say to all upper or all lower case or
whatever. I want to be able to select a few options from say a radio button
options panel, then hit start to traverse and rename the files. I was told
that it is a bad idea to put much code in an action listener cuz it is
suppose to slow the system down greatly. So how do I handle the event in a
way that I can invoke a thread class from my main class not from within the
action listener class? Or if I say call a thread class from within the
action listener will that be sufficient?

Thanks
Cleave



David Hilsee 08-02-2004 10:39 PM

Re: How do I start a thread by pressing a button? Simple hey?
 
"C-man" <iamcleaver@shaw.ca> wrote in message
news:I4zPc.16428$yT2.14347@clgrps13...
> Basically I have this program that will traverse a list of files in a
> directory and will rename each file say to all upper or all lower case or
> whatever. I want to be able to select a few options from say a radio

button
> options panel, then hit start to traverse and rename the files. I was told
> that it is a bad idea to put much code in an action listener cuz it is
> suppose to slow the system down greatly. So how do I handle the event in a
> way that I can invoke a thread class from my main class not from within

the
> action listener class? Or if I say call a thread class from within the
> action listener will that be sufficient?


It can be a bad idea to execute a long-running task without a background
thread because the GUI will stop responding while your long-running task
executes. If you want information on using threads in your GUI-based
application, read this:

http://java.sun.com/docs/books/tutor...c/threads.html

Pay close attention to the SwingWorker class and the link to general
information on threads.

--
David Hilsee



C-man 08-03-2004 02:02 AM

Re: How do I start a thread by pressing a button? Simple hey?
 
That is exactly the thing I am looking for. So now say you have another
button to stop the current time consuming task. how do you do that with the
swingWorker. Thanks


"David Hilsee" <davidhilseenews@yahoo.com> wrote in message
news:_umdnd-D8ZsAX5PcRVn-tQ@comcast.com...
> "C-man" <iamcleaver@shaw.ca> wrote in message
> news:I4zPc.16428$yT2.14347@clgrps13...
> > Basically I have this program that will traverse a list of files in a
> > directory and will rename each file say to all upper or all lower case

or
> > whatever. I want to be able to select a few options from say a radio

> button
> > options panel, then hit start to traverse and rename the files. I was

told
> > that it is a bad idea to put much code in an action listener cuz it is
> > suppose to slow the system down greatly. So how do I handle the event in

a
> > way that I can invoke a thread class from my main class not from within

> the
> > action listener class? Or if I say call a thread class from within the
> > action listener will that be sufficient?

>
> It can be a bad idea to execute a long-running task without a background
> thread because the GUI will stop responding while your long-running task
> executes. If you want information on using threads in your GUI-based
> application, read this:
>
> http://java.sun.com/docs/books/tutor...c/threads.html
>
> Pay close attention to the SwingWorker class and the link to general
> information on threads.
>
> --
> David Hilsee
>
>




David Hilsee 08-03-2004 03:09 AM

Re: How do I start a thread by pressing a button? Simple hey?
 
"C-man" <iamcleaver@shaw.ca> wrote in message
news:ZmCPc.17745$yT2.15887@clgrps13...
> That is exactly the thing I am looking for. So now say you have another
> button to stop the current time consuming task. how do you do that with

the
> swingWorker. Thanks


You can do a search for example code, but usually the approach taken
involves a boolean whose access is synchronized.

class MySwingWorker, etc {
private static class SynchedFlag {
private boolean flag = false;
public synchronized void set(){ flag = true; }
public synchronized boolean get() { return flag; }
}
private SynchedFlag stopFlag = new SynchedFlag();
public void requestStop() {
stopFlag.set();
}

}

When the button is pressed, set the flag (in the above code, that's the
requestStop() method). Then, have the background thread keep checking the
flag as it is doing its work (e.g. after every file it processes). When
stopFlag.get() returns true, give up the processing and exit the run()
method. There are other ways to do the same thing, but this is the way I
like, mostly because it looks the most readable to me.

--
David Hilsee




All times are GMT. The time now is 03:54 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.