Solved

forcing Java to process messages

Posted on 1997-08-18
4
1,055 Views
Last Modified: 2008-03-06
Hi!
 How would I force my program to look if there are any
 messages waiting in the queue and if any, proccess them.  Something like DoEvents in  VisualBasic or  Application.processMessages in Delphi.
 I.e. I have this loop I want to stop when user clicks the   stop button. Now, no messages are handled while the loop is
 running.
0
Comment
Question by:brat072297
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:msmolyak
Comment Utility
How about design like the following:

public class QueueProcessor {
private Queue theQueue;

public synchrinized Message getMesage() {
   while (theQueue.isEmpty()) {
      try {
         wait();
      } catch (InterruptedException e) {}
   }
   Message msg = theQueue.getMessage();  
   return msg;
}

public synchronized void putMessage(Message msg)
{
  theQueue.putMessage(msg);
  notify();
}
}

Classes Queue and Message are hypothetical classes whose meaning should be clear from the names and context.
0
 

Author Comment

by:brat072297
Comment Utility
I tried the "wait" solution myself, but it doesn't do the job.
I don't want my code to wait for the message.
If there's no "desired" event (not ANY event),  I want it to perform some other actions.
0
 

Author Comment

by:brat072297
Comment Utility
Maybe I wasn't clear enough. I want may app to see if the button
was clicked while the loop is running. If it was clicked then exit the loop otherwise continue. But Java doesn't allow handleEvent method to be called while the loop is running. As I said this was(is) trival in VB and Delphi, but in Java........
0
 
LVL 5

Accepted Solution

by:
msmolyak earned 100 total points
Comment Utility
I still believe that you need at least two therads. If you want the main thread to run while the other thread processes the button click, let it run in a try block with InterruptedException in the catch part. Thus when the button is clicked, you can interrupt the running thread and handle that event in the catch block.

Is that what you are looking for?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

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

10 Experts available now in Live!

Get 1:1 Help Now