Results 1 to 4 of 4

Thread: Infinite loop

  1. #1
    F.S. is offline Member
    Join Date
    Feb 2012
    Location
    Bruges, BELGIUM
    Posts
    16
    Rep Power
    0

    Default 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;
        }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default 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!

  3. #3
    F.S. is offline Member
    Join Date
    Feb 2012
    Location
    Bruges, BELGIUM
    Posts
    16
    Rep Power
    0

    Default Re: Infinite loop

    Quote Originally Posted by KevinWorkman View Post
    Why don't you just use a Swing Timer?
    Because I have never heard of it before . This is all relatively new to me.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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.

    ** This space for rent **

Similar Threads

  1. infinite loop
    By javapink in forum New To Java
    Replies: 19
    Last Post: 03-06-2011, 02:28 AM
  2. how to end infinite loop
    By search4survival in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 08:59 AM
  3. Infinite loop
    By jDennis79 in forum New To Java
    Replies: 7
    Last Post: 08-13-2010, 11:45 PM
  4. Infinite Loop
    By bosoxfan in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 01:34 AM
  5. Infinite Loop
    By rclausing in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 10:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •