Solved

need help with swing and multithreading (gui lockup)

Posted on 2003-10-31
4
338 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
  • 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now