Results 1 to 6 of 6
  1. #1
    Ben1 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default Stopping a thread using a stop button - GUI

    Hi all,
    I've got an app all made, looking lovely.

    But... I have a GUI that has start and stop buttons. Start button kicks off a new thread to capture packets. Working fine.

    The stop button, this needs to stop capturing the packets but keep the actual program running - so System.exit(); isn't suitable (different button for that).

    I've read up on it and it seems like interrupt is my best bet but I can't get it to work. Is there just a simple line that I can put in to stop the thread?

    Thanks
    Ben

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

    Default

    You could check a boolean in your Thread and return when it's false. Then just update that variable when the stop button is pressed.
    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
    Ben1 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    I like it.

    That works and it stops capturing packets. But then if I click the start button again I get a load of errors...

    **
    Exception occurred during event dispatching:
    java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Thread.java:638)
    at GUI.IMsafeGUI.StartActionPerformed(IMsafeGUI.java: 497)
    at GUI.IMsafeGUI.access$1000(IMsafeGUI.java:34)
    at GUI.IMsafeGUI$12.actionPerformed(IMsafeGUI.java:44 5)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6267)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:603 2)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:178)
    at java.awt.Dialog$1.run(Dialog.java:1046)
    at java.awt.Dialog$3.run(Dialog.java:1098)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.Dialog.show(Dialog.java:1096)
    at java.awt.Component.show(Component.java:1563)
    at java.awt.Component.setVisible(Component.java:1515)
    at java.awt.Window.setVisible(Window.java:842)
    at java.awt.Dialog.setVisible(Dialog.java:986)
    at GUI.IMsafeGUI$14.run(IMsafeGUI.java:643)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

    **



    Any ideas?


    Thanks
    Ben

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

    Default

    I can't really comment on your code as you haven't posted any :p

    If you want to post code, make sure it's as small as possible- preferably in SSCCE form.

    But I would guess that you're trying to start a Thread that has already been started and completed. Chances are you want to start a new Thread each time you press Start (making sure the old one has stopped first), possibly using the same instance of Runnable.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Ben1 is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    Yeah, it's throwing the error because I'm trying to start a thread that's already running. And I know I need to stop the first one before I can start the new one... that was my original question :P

    The code below is the part that contains your suggested boolean (toRun) and is the run thread that everything else spawns from. Stopping this class from running will stop everything that follows it.

    Java Code:
    public class IMsafe {
    
        public static boolean toRun = true;
    
        public static void run(int selectedIndex) throws IOException {
    
            NetworkInterface[] devices = JpcapCaptor.getDeviceList();
            JpcapCaptor captor = JpcapCaptor.openDevice(devices[selectedIndex], -1, false, 20);
            ActiveThreads activeThreads = new ActiveThreads();
            int count = 0;
            Packet tempPacket = captor.getPacket();
            for (selectedIndex = 0; selectedIndex < 10; selectedIndex++) {
                if (toRun == true) {
                    if (tempPacket != null) {
    
                        //count++;
                        //System.out.println("still monitoring: " + count);
                        new Thread(new CapturePackets(captor, tempPacket, activeThreads)).start();
                        tempPacket = captor.getPacket();
                        selectedIndex = 0;
                    } else {
                        tempPacket = captor.getPacket();
                        selectedIndex = 0;
                    }
                } else{
                    return;
                }
            }
        }
    }
    I THINK the problem above is that by the time the class started capturing packets the thread has already started etc. But I can't put the boolean check above the for loop because obviously once it's in the loop it won't stop until the NIC stops capturing packets... never gonna happen.

    Any suggestions?

    Ben

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

    Default

    I honestly don't really understand what's going on in that code, as it contains a bunch of external classes- which is where that SSCCE would come in handy.

    But for what it's worth, here is the short example I came up with:

    Java Code:
    public class ThreadTest {
     
    	public static void main(String[] args) throws Exception {
    	
    		Runnable r = new Runnable(){
    			public void run(){
    				System.out.println("Running.");
    			}
    		};
    		
    		new Thread(r).start();
    		Thread.sleep(1000);
    		new Thread(r).start();
    	}
    }
    Your check against the boolean would go inside the run() method. Also, for what it's worth, the method of using a boolean to stop the Thread is actually described in the documentation for the deprecated Thread.stop() method.

    Thread (Java Platform SE 6)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Stopping a thread
    By Arne in forum Threads and Synchronization
    Replies: 9
    Last Post: 10-21-2010, 10:26 AM
  2. Stopping a thread
    By userj2ee in forum New To Java
    Replies: 3
    Last Post: 08-13-2010, 07:57 PM
  3. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 12:50 PM
  4. Replies: 11
    Last Post: 01-26-2009, 12:22 AM
  5. stopping thread...using flags
    By rstepler in forum New To Java
    Replies: 1
    Last Post: 07-31-2008, 09:36 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
  •