Results 1 to 6 of 6
- 01-27-2011, 02:39 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 16
- Rep Power
- 0
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
- 01-27-2011, 03:03 PM #2
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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 01-27-2011, 03:33 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 16
- Rep Power
- 0
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
- 01-27-2011, 03:41 PM #4
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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 01-27-2011, 03:52 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 16
- Rep Power
- 0
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; } } } }
Any suggestions?
Ben
- 01-27-2011, 05:21 PM #6
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(); } }
Thread (Java Platform SE 6)How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
Similar Threads
-
Stopping a thread
By Arne in forum Threads and SynchronizationReplies: 9Last Post: 10-21-2010, 11:26 AM -
Stopping a thread
By userj2ee in forum New To JavaReplies: 3Last Post: 08-13-2010, 08:57 PM -
Use stop button to stop moving (stop timers) on JPanel
By mneskovic in forum New To JavaReplies: 3Last Post: 07-23-2010, 01:50 PM -
Stopping a running thread in a multithreaded environment
By SUSHMA50 in forum Advanced JavaReplies: 11Last Post: 01-26-2009, 01:22 AM -
stopping thread...using flags
By rstepler in forum New To JavaReplies: 1Last Post: 07-31-2008, 10:36 PM
Bookmarks