Solved

need help with swing and multithreading (gui lockup)

Posted on 2003-10-31
4
343 Views
Last Modified: 2011-09-20
I have a multithreaded application and am using swing as my interface.  My threads communicate with each other through queues that are class objects of the base class the threads are derived from.

I want to click a menu item to send a message to one of the threads and wait for a response in the background and then reenable the menu item it has been recieved. I've read the sun doc's and thought i could use invokelater ()., but my gui locks up. cCan anyone help. herses the code

// heres the event handler for menu item
public void actionPerformed(ActionEvent e)
  {

   adaptee.OffHook_actionPerformed(e);
   // disable menu item
   adaptee.OffHook.setEnabled(false);

 
   Runnable doWorkRunnable = new Runnable()
   {
     public void run()
       {
       ProcessConsleMsg ();
       }
    };

  // I thought this would run in background thread
   SwingUtilities.invokeLater (doWorkRunnable);
  }


// sends a message to control queue and waits for response
// console queue
  public void ProcessConsleMsg ()
  {
   Msg zMsg;

   zMsg = new Msg ();
   zMsg.SetMsgType(OFF_HOOK);

   // send message to crontrol task
   ConsleTsk.SendCtrlMsg(zMsg);

    // wait for response to return in consle queue
    // on purpose I'm not send back a response, locking up gui
   // wy doesn't this porocess run in background?
    while (ConsleTsk.ConsleQue.GetMsg()   == null);

    adaptee.OffHook.setEnabled(true);

   }

}
0
Comment
Question by:BlackRussian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9661344
> // I thought this would run in background thread
>  SwingUtilities.invokeLater (doWorkRunnable);

No that says to run it in the swing thread.

You need to wait in a new Thread, and once done then call invokeLater from tyhat thread to notify your gui.
0
 

Author Comment

by:BlackRussian
ID: 9661646
Can you point me to some example code. I was using the bingo example in sun docs.
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 9661663
something like:

public void actionPerformed(ActionEvent e)
  {

   adaptee.OffHook_actionPerformed(e);
   // disable menu item
   adaptee.OffHook.setEnabled(false);

 
   Runnable doWorkRunnable = new Runnable()
   {
     public void run()
       {
       ProcessConsleMsg ();
       }
    };

   new Thread(doWorkRunnable).start();
  }


// sends a message to control queue and waits for response
// console queue
  public void ProcessConsleMsg ()
  {
   Msg zMsg;

   zMsg = new Msg ();
   zMsg.SetMsgType(OFF_HOOK);

   // send message to crontrol task
   ConsleTsk.SendCtrlMsg(zMsg);

    // wait for response to return in consle queue
    // on purpose I'm not send back a response, locking up gui
   // wy doesn't this porocess run in background?
    while (ConsleTsk.ConsleQue.GetMsg()   == null);

    EventQueue.invokeLater(new Runnable()
    {  public void run() { adaptee.OffHook.setEnabled(true); }});

   }

}
0
 
LVL 92

Expert Comment

by:objects
ID: 9661803
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question