• Post Reply Bookmark Topic Watch Topic
  • New Topic

if--key pressed--return  RSS feed

 
carl sjostrom
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to return from a long function if the user presses any key.

This is sort of what my program looks like...


I have been looking in keyEvent, concurrence and threads etc. but it seems
like one should be able to just put in an if statement that asks if a key
has been pressed and then simply call return.

Any ideas on the right way to do this would be most welcome.

Carl
[ February 07, 2007: Message edited by: carl sjostrom ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need two threads (at least) to get this done. Any time you start a long running process in a Swing app - more than a second or three - it's good to get that off to another thread so we should probably have one even without the "stop" keystroke requirement.

We can easily stop the other thread if it's looping, but it can be hard to stop the other thread if it's blocking on IO or some other uninterruptable operations.

The cruncher calls all it's heavy lifting in the run() method. If we're lucky it's in a loop and stopCrunching() can set a variable that makes it exit the loop. Otherwise it can be tricky to stop.

At the end of the run() method, the cruncher probably wants to put the results back on screen. Updating Swing components from the cruncher thread will not work reliably and correctly. So the cruncher needs to create yet another runnable with code to update the UI and call SwingUtilities invoke later.

So we set out several challenges. Create and start a thread, try to stop a thread, and report the results. Any of them sound like something you can do?
 
carl sjostrom
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> Any of them sound like something you can do?

No, but I got all day and plenty of google left...

Thanks for giving me a place to start looking and learning.

Carl
[ February 07, 2007: Message edited by: carl sjostrom ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!