Results 1 to 4 of 4
Thread: Infinite loop
- 03-07-2012, 03:38 PM #1
Member
- Join Date
- Feb 2012
- Location
- Bruges, BELGIUM
- Posts
- 16
- Rep Power
- 0
Infinite loop
So here's the problem.
I know this code creates an infinite loop, but that is actually what I want.
The idea is that by pressing a start button this infinite loop is activated and repeated every 5 seconds. This keeps on going until I press the stop button.
Offcourse when an infinite loop is set in motion, the applet doesn't respond to anything(like pressing the stop button)because it wants to finish the while loop first.
Is there anyway that I can make it possible to push the stop button while the infinite loop is set in motion?
Java Code:private void Button_Start_Time_LoopActionPerformed(java.awt.event.ActionEvent evt) { loop=true; while(loop==true){ ... //the code here is not important for my problem Thread.sleep(5 * 1000); } } private void Button_Stop_Time_LoopActionPerformed(java.awt.event.ActionEvent evt) { loop=false; }
- 03-07-2012, 03:44 PM #2
Re: Infinite loop
Why don't you just use a Swing Timer?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-09-2012, 11:59 AM #3
Member
- Join Date
- Feb 2012
- Location
- Bruges, BELGIUM
- Posts
- 16
- Rep Power
- 0
- 03-09-2012, 12:52 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
Re: Infinite loop
You need to read up on concurrency in a Swing application.
The Swing GUI runs on its own thread (the Event Dispatch Thread) in which all events are handled.
Your loop above (which is run in an event handler) is also running in this thread, so when it goes into its infinite loop you lock up the EDT...which means nothing else can be done on that thread, so the GUI locks up.
The Swing Timer sticks the timing task off on another thread.
A SwingWorker (mentioned in the link above) also fires off another thread, in that case one in which you can do lots of other logic.Please do not ask for code as refusal often offends.
Similar Threads
-
infinite loop
By javapink in forum New To JavaReplies: 19Last Post: 03-06-2011, 02:28 AM -
how to end infinite loop
By search4survival in forum New To JavaReplies: 14Last Post: 10-25-2010, 08:59 AM -
Infinite loop
By jDennis79 in forum New To JavaReplies: 7Last Post: 08-13-2010, 11:45 PM -
Infinite Loop
By bosoxfan in forum New To JavaReplies: 3Last Post: 02-22-2010, 01:34 AM -
Infinite Loop
By rclausing in forum New To JavaReplies: 2Last Post: 01-23-2010, 10:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
. This is all relatively new to me.

Bookmarks