Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

forcing Java to process messages

Posted on 1997-08-18
4
Medium Priority
?
1,212 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
[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
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1225278
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
ID: 1225279
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
ID: 1225280
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 200 total points
ID: 1225281
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

610 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