Solved

need help with swing and multithreading (gui lockup)

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

738 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