Controlling a thread from the gui

Hi ive written a small piece of code that demonstrates the problem im having:
package Util;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Monitor implements ActionListener {
     Mon mon = new Mon();
     public Monitor() {
          JFrame frame = new JFrame();
          JButton start = new JButton("Start");
          JButton pause = new JButton("Pause");
          pause.addActionListener(this);
          start.addActionListener(this);
          frame.setLayout(new FlowLayout());
          frame.add(start);
          frame.add(pause);
          frame.setVisible(true);
          frame.pack();
     public void actionPerformed(ActionEvent e) {
          if (e.getActionCommand() == "Start") {
               mon.start();
          } else {
               mon.pause();
     public static void main(String args[]) {
          new Monitor();
class Mon extends Thread {
     public void run() {
          while (true) {
               System.out.println("Running...");
               try {
                    sleep(1000);
               } catch (InterruptedException e) {
                    e.printStackTrace();
     public synchronized void pause() {
          try {
               wait();
          } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
}I was wondering whether anyone could help me fix this or point out where im going wrong. Whenever the pause button is pressed, i want the thread to temporarily stop printing out "Running" until start is pressed again. At the mo, when i press the pause button, the gui locks up and the thread continues to print out running.
Cheers.

Thanks da.futt your advice helped me solve the problem, working example:
package Util;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Monitor implements ActionListener {
     Mon mon = new Mon();
     public Monitor() {
          JFrame frame = new JFrame();
          JButton start = new JButton("Start");
          JButton pause = new JButton("Pause");
          pause.addActionListener(this);
          start.addActionListener(this);
          frame.setLayout(new FlowLayout());
          frame.add(start);
          frame.add(pause);
          frame.setVisible(true);
          frame.pack();
     public void actionPerformed(ActionEvent e) {
          if (e.getActionCommand() == "Start") {
               try {
                    mon.start();
               } catch (Exception ex) {
                    mon.restart();
          } else {
               mon.pause();
     public static void main(String args[]) {
          new Monitor();
class Mon extends Thread {
     private boolean running = true;
     public void run() {
          while (true) {
               if (running) {
                    System.out.println("Running...");
                    try {
                         sleep(1000);
                    } catch (InterruptedException e) {
                         e.printStackTrace();
               } else {
                    synchronized (this) {
                         try {
                              wait();
                         } catch (InterruptedException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
     public void restart() {
          running = true;
          synchronized (this) {
               notify();
     public void pause() {
          running = false;
          synchronized (this) {
}

Similar Messages

  • Is it possible to seperate an ActiveX control from the GUI thread?

    I have an activeX control (*ocx) that we use for communicating with external instruments. We are running into some difficulty because if the communication with the instrument is held up it can take up to 30 seconds to timeout and our LabVIEW user interface is frozen in the mean time. The VI that contains the control itself does not have to be visible and all the subVIs that use the ActiveX reference have been set to run in the "instrument" execution system thread, but the GUI still freezes. Its my theory that since the ActiveX control itself is in a container and part of a front panel, all refernces to it must be executed in the GUI thread. I just wanted to confirm that this was the case. Also if anyb
    ody would like to suggest any work arounds, feel free. The only idea I've had so far was to move all use of the ActiveX control to external code, which is not a very attractive solution for us.

    ....Also if anybody would like to suggest any work arounds,
    > feel free. The only idea I've had so far was to move all use of the
    > ActiveX control to external code, which is not a very attractive
    > solution for us.
    ActiveX controls are loaded into the UI thread and all interactions with
    them, property nodes, and invoke nodes, have to take place in the UI
    thread/main thread of LV. So, setting the VIs to run in the
    instrumentation or other execution systems, doesn't do much good since
    each call to the property or invoke will just have to switch back.
    What other choices do you have? If the control doesn't have a UI
    anyway, it would improve matters if the control were simply an
    automation server. The automation server can be set to run in a single
    thr
    eaded apartment, or a MT apartment. Changing it to MT means that you
    will have to deal with protecting the code from being called from
    different threads, and having those calls interleaving and competing
    with one another. When you change it to be MT, it can tie up other
    threads in LV and leave the UI thread alone. Another alternative is to
    change the ActiveX control to spin up its own thread and have a method
    that initiates, and one that reads the results or returns the status.
    Greg McKaskle

  • How do i control my music from the lock and control screen?

    I Dont know if theres a setting i have to change, but i cant control my music from the control screen nor can i control it on the lock screen while the music is already playing. Is this a settings problem or is it just not working for my iphone 5?

    I'm having this same issue on the 4s.

  • How to do you control lower levels from the Stage?

    How to you control lower levels from the Stage?
    I have a button on the main stage and I have a symbol that has another symbol inside that I want to control that timeline or label.
    I have had success with going one level deep but not two or three deep.
    sym.getSymbol("symbol01").stop("label01");
    Any help would be great!
    Thanks.

    Can you explain the hierachy of your symbols? By this I mean, name of your symbol and any symbol inside and any symbol inside nested symbol and what event you are triggering from where?

  • The main develop module controls have disappeared from the develop window.  How do I recover them?

    The main develop module controls have disappeared from the develop window.  How do I recover them?  I'm working with Lightroom 5.3.  The missing controls are the sliders for exposure, contrast, clarity, etc.  I must have inadvertantly clicked something that made them disappear but nothing I've tried brings them back.  I've never had this experience in the years I've been using Lightroom (since version 1.1).

    Right-click on any other item Tone Curve for ex., and check on "Basic" if that's what you are missing.
    If you miss the complete Develop module right-click any other item  Library for ex and put a check on Develop

  • The volume control has disappeared from the music app on my iPhone and i can't play any music how do i fix this?

    The volume control has disappeared from the music app on my iPhone and i can't play any music how do i fix this?

    Try a reset: Simultaneously hold down the Home and On buttons until the device shuts down. Ignore the off slider if it appears. Once shut down is complete, if it doesn't restart on it own, turn the device back on using the On button. In some cases it also helps to double click the Home button and close all apps from the tray BEFORE doing the reset.

  • How to run a shell script from the GUI?

    This is probably a dumb question...
    How do I run a shell script from the GUI? I've been told to double click it but when I do, it opens as a text file.

    The behavior you describe is that used by the KDE and GNOME desktops of Linux.
    Under OS X, if you make a script then mark it as executable, double-clicking on it in the Finder will not execute it. Actually, it uses a rather complex algorithm ([summarized here|http://arstechnica.com/reviews/2q00/macos-qna/macos-x-qa-2.html]) to determine what to do with it. This is implemented in Mac OS X' LaunchServices framework (incidentally, the associations are cached in /Library/Caches/com.apple.LaunchServices*.csstore and ~/Library/Caches/com.apple/LaunchServices*.csstore). You can read the details in the developer docs about LaunchServices.
    Anyway, in short, the suffix '.command' is a built-in type in the LaunchServices network that identifies a shell script. If you run
    /System/Library/Frameworks/ApplicationServices.framework/Frameworks/LaunchServic es.framework/Support/lsregister -dump
    ... it will tell you as much.

  • Flash audio controls is gone from the Windows audio mixer.

    Flash audio controls is gone from the Windows audio mixer. The sound is, but I can not control it separately from the browser. Previously, there was a separate flash and a separate browser. When I use FF3. *. Flash Player audio option was, but the new FF versions do not. For me it was very comfortable and loved.
    How can I repay such opportunity? and whether there is still a possibility?

    Thank cor-el, but it did not help. I tried everything ( I installed it all over again, several times , clean cookies I'm using [[BetterPrivacy 1.68|BetterPrivacy 1.68]] add-ons clean cookies ... ), but the problem is not in the Flash Player to crash. It works. But I can not manage alone plugin container volume and alone audio browser.
    I am not a programmer, so I do not know how I can change it yourself.
    Maybe someone already has it done? I would be grateful if you divide.
    Sorry for my bad English. Here's screen shot if I can not clearly say:
    http://img94.imageshack.us/img94/707/ffsupport.jpg

  • Controlling print quality from the touchscreen on the MG6320

    Hello, I have a two-part question. I'm interested in controlling print quality from the touchscreen display.
    1. Can I set print quality (i.e. - choose draft/fast mode) from the touchscreen display?
    2, if I can and do set it there, will my selection apply to all future print jobs until I change the setting again, or will it just apply to the one print job in the queue at the time, etc?
    I hope my questions are clear. Many thanks for your help.
    Solved!
    Go to Solution.

    Hi Ralp51,
    The print quality of documents printed from your computer is controlled be the printer driver on the computer.  The quality cannot be adjusted from the PIXMA MG6320 itself.
    Did this answer your question? Please click the Accept as Solution button so that others may find the answer as well.

  • Threads from the corba threadpool can take a long time killing

    I'm using the bounded threadpool (well, actually, I have changed the default unbounded threadpool to a bounded one and injected it using the endorsed dir mechanism - but the behaviours was the same before I did that - except that I then had 32000 threads in some cases) for handling corba events I receive from an ORB. I can see the number of threads increase and decrease as expected based on the load.
    However, sometimes everything hangs: The GUI freezes, I start loosing network packages and other unfortunate things occur. This coincides with the removal of threads (according to YourKit), e.g. last evening the removal of 14 threads took ~30 seconds and during that time absolutely nothing else appeared to happen. My two other Java processes were also for all intents and purposes dead (also according to YourKit) during that period.
    Is this related to http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=1194877 in any way? This seems such a long standing issue that I find it hard to understand that this has not been resolved in any way. Is there a way around this problem?
    If it's not related, what can it then be? I'm not even sure where to look? Is it the Java implementation, the linux kernel, some combination with the hardware?
    I'm using SUNJava 1.5 u10 on Fedora Core 5 linux kernel 2.6.18-1.2239.fc5 on AMD64 hardware.

    Follow-up to myself since no-one else has replied:
    Could it be related to this bug [http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6372405] and if it is, does the poster mean JDK 6, NOT 7?

  • Waiting on a user's response (from the gui)

    This should be a really simple problem, but I can't work it out.
    I have two classes, my Engine and my Gui. The Engine tells the Gui what to display, the gui displays it and has methods to fire events when a user presses a button. I want to do something like the following
    public class Engine {
      Gui gui;
      String[] pictures;
      // ... a bunch of stuff
      public void showPictures {
        for (int i=0; i<pictures.length; i++)
          gui.showPicture(pictures);
    // wait on the user's response
    Here's the question: preferably without using threading (though I could if I had to), how can I wait on a user's response from within a loop?
    I considered having a boolean flag "hasResponded" in the gui, and then having a do-while loop just spinning until it saw that gui.hasResponded was equal to true, but it seems that this would add a lot of unnecessary processing load.
    Any thoughts?
    Thanks!

    don1983p wrote:
    I have the same question "So question is still just the simple one above: can I (easily) wait on some event, like the gui recording an action or anything else, from within a loop?"...If you have a question to ask, do so in a new thread. Don't resurrect a long-dead thread. Sure you can refer to it and even link to it, but let it rest in peace.
    Any new ideas?Why would you expect the answer to change? You don't do this via a loop as GUIs don't work like this. Have you gone through the Swing tutorials? If not, please go through them as they'll show you how to do this correctly.
    I wrote a WHILE loop, the condition of the loop being the values (getActionCommand) of some buttons being pressed. How can I wait for another button being pressed so I can get out of the loop?You don't. If you need to time a response, then use a Swing Timer. But most importantly, read the Swing tutorials.

  • Control LabVIEW DLL from LabWindows GUI only

    Hello,
    I have been developing an application in LabWindows/CVI and have recently built some auxiliary applications in LabVIEW.  My goal is to make the LabVIEW applications into a DLL and call them from the LabWindows app; however, one of my LabVIEW VIs has a front panel control and a local variable to stop a pair of asynchronous while loops.  What I would like to do is remove/hide the front panel of this LabVIEW VI and control the VI from the LabWindows GUI exclusively.  Is this possible?
    Thanks!
    Slowpoke
    Slowpoke
    CLAD (believe it or not!), using LabVIEW 8.5
    They don't call me "Slowpoke" for nothin'!

    Hi, David,
    Once I figured out how to make a front panel control into a parameter, I had what must be a common reaction among LabVIEW users: Can it really be that easy?  Thank you for your reply and your assistance.
    Slowpoke
    Slowpoke
    CLAD (believe it or not!), using LabVIEW 8.5
    They don't call me "Slowpoke" for nothin'!

  • Feature Request: Export and Import all or part of vCenter configuration from the GUI

    One of the challenges we face is how to recover a large vCenter to a different VM, without having to restore any databases or use vCenter VMs and databases on replicated storage. We currently have a PowerCLI based process for exporting vCenter configurations - vCenter settings, roles/privs, alarm definitions, datacenters/clusters/hosts - to an XML file, which can then be used to import that vCenter configuration into a recovery vCenter. Three part process - export the vCenter configuration, import the settings and structure (datacenters, clusters, alarms), then import the hosts. This last step pulls in the hosts once by one from the original vCenter, complete with their running VMs and once all hosts in a cluster are imported creates the required Resource Pools and moves the VMs in to them. A subsequent script moves all the VMs into appropriate blue folders based on their CMDB classification/OS.
    The above process is OK, but has a number of faults:
    It's all or nothing for a migration, unless there is some serious editing of the XML dump to extract, for example, a specific datacenter or cluster, which could if incorrectly done result in configuration issues, hosts not being imported, etc.
    It can't (at least not in vc4.x) migrate dvSwitches. If we have those in a datacenter, we're going to have to reconfigure the hosts and VMs back onto conventional vSwitches
    We'd really like to see a native tool built in to vSphere Client or the Web Client that will allow all or part of a vCenter configuration to be exported to an XML or other flat file and then imported into another vCenter in one easy step, taking running VMs, dvSwitches - the lot. In the same way you can export host and vCenter logs with a few clicks, it should be possible to do something similar for vCenter configurations. This would be useful for a number of things - vCenter recovery, vCenter upgrade, individual datacenter or cluster migration.

    I got the answer: http://forum.java.sun.com/thread.jspa?threadID=577129

  • I can't uninstall; tried from control panel and from the uninstaller help file.

    all explained above. nothing will remove this program from my computer...windows 8.1

    Hey bmatthewb
    Sorry to hear you are not enjoying firefox as much as we are, and it seems your firefox doesnt want you to part either.
    Try deleting it manually:
    Delete the Firefox installation folder, which is located in one of these locations, by default:
    Windows:
    C:\Program Files\Mozilla Firefox
    C:\Program Files (x86)\Mozilla Firefox
    Once done empty recycle bin and see if it is then listed in your programs from the control panel.
    Hope this helps and get back to us so it is easier for other users to find the solution if they have the same prob :)
    Brad

  • Removing Threads from the Process

    Hi,
    I have a client/server application.Only 6 clients can be connected to the server.
    Server updates the clients at a specific time interval. My thread count is keep on increasing even if I close some clients.I am able to see that from the Task manager-process.
    Is there any way I can reduce the thread count if a client is killed.Is it due to JVM problem?
    Regards,
    vimal

    Well, threads only close when the run method exits. So I guess your connection threads are waiting indefinitely on a socket call to the client which has disappeared. Have you set a timeout on the socket? That should cause a lost connection to throw a SocketTimeoutException (from the read/write call). Catch this exception and close the service thread gracefully.

Maybe you are looking for

  • What is business systems and services?

    Hi Experts, What is business systems and services and where are they prefered? and tell me the diff between them... Regards, Mahakrishnan T.

  • How to : Buy qty 12 total of A, B, or C and all items (D,E, F,...) get 5%

    For a long time SD'r this has me stumped. No problem doing a group condition with a scale qty where if total 12 is ordered of material A, B, or C I am getting a 5% discount on material A, B. C but the sales group wants a promo where all line items ge

  • Building shared libraries on linux with SunStudio

    Hi, I installed the sunstudio-compilers on my system (SuSE 10.0/x86-64) and try to build a shared library. For my (c++-)library I use autoconf/automake, so I hoped it to be trouble-free, but it is not. I configured my library with: ./configure CC=cc

  • Urgent JMS Problem,Help!!

    I meet a urgent problem about JMS,I use wls61 sp1 but when serverl MDB subscribed to one topic,problem accur and not all of the subscriber seems to receive the message,I know that sp2 have slove the bugs.but I just want to get the patch of jms for wl

  • Inserting a Video into Dreamweaver

    There is an option to insert a Flash movie into Dreamwaever CS3 but there is not an option to insert a Windows Media File or file with extension .WMV , Could somepne please explain to me how I accomplish this. SPECTRA.