Solved

need help with swing and multithreading (gui lockup)

Posted on 2003-10-31
4
337 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
Comment Utility
> // 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
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

728 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

9 Experts available now in Live!

Get 1:1 Help Now