brat072297
asked on
forcing Java to process messages
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.processMessage s 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.
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.processMessage
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.
ASKER
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.
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.
ASKER
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........
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........
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.