Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Loop help

  1. #1
    New Coder
    Join Date
    Mar 2005
    Location
    Indiana (USA)
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Loop help

    Alright, in my program I have a GUI and when you click a button a loop starts. Is there anyway to isolate that loop so that another button can be click to stop it? I have tried putting the loop in a separate thread, but I don't think that is what I need (or I did it wrong and it still ran without error)

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Without seeing code it's hard to say. But putting the loop in another thread waiting for a signal from your parent thread _should_ work. So if there is still an error then it is most likely some type of coding error, which we will need to see code for.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #3
    New Coder
    Join Date
    Mar 2005
    Location
    Indiana (USA)
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I guess code would be helpful wouldn't it. Alrighty, here we go.

    This is the function that I need to have in a different thread.
    Code:
    private class doPrac implements Runnable
    	{
    		public void run() {
    			Random wheel = new Random();
    			while(!stopLoop) {
    				doWait();
    				doSound(wheel.nextInt(numBoxes));
    			}
    		}
    		
    	}
    This is where my listener goes for the button click:
    Code:
    private class Practice implements ActionListener {
    
    		public void actionPerformed(ActionEvent arg0) {
    			new doPrac().run();
    		}
    		
    	}
    In theory the loop is in a different thread. Oh, and stopLoop is a class wide variable that is changed by the stop button.

  • #4
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    If I understand it right, its actually not in another thread. You should be able to create a thread and pass your doPrac object to it as it already implement the Runnable interface. Should only need to create the thread and then call threan.start().

  • Users who have thanked brad211987 for this post:

    chiefbutz (07-09-2008)

  • #5
    New Coder
    Join Date
    Mar 2005
    Location
    Indiana (USA)
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts
    That did it. All I had to do was replace
    Code:
    new doPrac().run();
    with

    Code:
    new Thread(new doPrac()).start();
    thank you for your help. I now know how to do threads (I had never used them before)


  •  

    Posting Permissions

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