Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » readAndDispatch is blocking
readAndDispatch is blocking [message #464551] Thu, 24 November 2005 16:25 Go to next message
Eclipse UserFriend
Originally posted by: mimhof.ch.csc.com

I have a small application with a menu and a canvas wich displays
real time data. (a new image is painted every 100ms. This repaint is
made by a client thread).
If I click on the menu bar and the menu is opened, the refresh stops!!
It seems, that the readAndDispatch() call is blocking!
How can I make this menu actions non-blocking?

Michael
----------------- Code Snippet ---------------
The following thread is started with
display.asyncExec(new TestDataThread());

public class TestDataThread extends Thread
{
public void run()
{
// Print new data
try
{
while (display.readAndDispatch());
Thread.sleep(100);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
Re: readAndDispatch is blocking [message #464552 is a reply to message #464551] Thu, 24 November 2005 17:07 Go to previous messageGo to next message
Stefan Langer is currently offline Stefan LangerFriend
Messages: 236
Registered: July 2009
Senior Member
Just as a side node: You are aware that you are blocking the ui thread
with thread.sleep(100) not your own thread?

Why are you handling the event dispatching instead of simply updating
your canvas and calling redraw or something similar in the event thread?

Regards
Stefan

Michael Imhof wrote:
> I have a small application with a menu and a canvas wich displays real
> time data. (a new image is painted every 100ms. This repaint is
> made by a client thread).
> If I click on the menu bar and the menu is opened, the refresh stops!!
> It seems, that the readAndDispatch() call is blocking!
> How can I make this menu actions non-blocking?
>
> Michael
> ----------------- Code Snippet ---------------
> The following thread is started with display.asyncExec(new
> TestDataThread());
>
> public class TestDataThread extends Thread
> {
> public void run() {
> // Print new data
> try
> {
> while (display.readAndDispatch());
> Thread.sleep(100);
> } catch (InterruptedException e)
> { e.printStackTrace();
> }
> }
> }
>
Re: readAndDispatch is blocking [message #464553 is a reply to message #464552] Thu, 24 November 2005 17:19 Go to previous messageGo to next message
Alex Blewitt is currently offline Alex BlewittFriend
Messages: 946
Registered: July 2009
Senior Member
You've got an extra ; after your while loop. The loop is endlessly repeating, and not actually hitting the Thread.sleep(), so presumably it acts as if it's locked up and doesn't do anything.

Always using { and } even for single lines (like this one) will help avoid this problem in the future.

Alex.
Re: readAndDispatch is blocking [message #464557 is a reply to message #464551] Thu, 24 November 2005 18:54 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
> display.asyncExec(new TestDataThread());

1) This is not creating a new Thread. You are running the code in
TestDataThread.run() inside the UI thread. Therefore your Thread.sleep() is
blocking the UI thread. It just happens that Thread is a subclass of
Runnable. The display.asyncExec API expects a Runnable, not necessarily a
Thread and it will not spawn a new thread.
2) You are using asyncExec. The asynchronous exec does not run unless the
UI event loop is idle. The UI event loop is not idle while the user is
moving the mouse up and down the menu. If you want the code to run
immediately in the UI thread you should use display.syncExec().

In the example below, the canvas redraws in a different color every time it
is asked to redraw. It is forced to redraw from a second thread that wakes
up every 100 ms. Notice that the canvas continues to redraw even when the
menu is open and the user is moving the mouse quickly over the menu items.

public static void main (String [] args) {
final Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
final Canvas canvas = new Canvas(shell, SWT.BORDER);
canvas.addListener(SWT.Paint, new Listener() {
int index = 0;
Color[] colors;
public void handleEvent(Event e) {
if (colors == null) {
colors = new Color[] {
display.getSystemColor(SWT.COLOR_RED),
display.getSystemColor(SWT.COLOR_BLUE),
display.getSystemColor(SWT.COLOR_GREEN),
display.getSystemColor(SWT.COLOR_YELLOW),
display.getSystemColor(SWT.COLOR_CYAN),
display.getSystemColor(SWT.COLOR_MAGENTA),
display.getSystemColor(SWT.COLOR_DARK_BLUE),
};
}
e.gc.setBackground(colors[index]);
e.gc.fillRectangle(canvas.getClientArea());
index = (index + 1) % colors.length;
}
});

Menu menu = new Menu (shell, SWT.POP_UP);
for (int i = 0; i < 20; i++) {
MenuItem item = new MenuItem (menu, SWT.PUSH);
item.setText ("Popup "+i);
}
canvas.setMenu (menu);
shell.open ();

Thread newThread = new Thread() {
public void run() {
// Print new data
Display display = Display.getDefault();
try {
while (!display.isDisposed()) {
display.syncExec(new Runnable() {
public void run() {
// All code in this
run method is executed in the UI thread
// therefore do not
put slow non-ui code here!!!!
if
(canvas.isDisposed()) return;
canvas.redraw();
}
});
sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
newThread.start();

while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}

"Michael Imhof" <mimhof@ch.csc.com> wrote in message
news:009fde1a25477a01217e034126dee81a$1@www.eclipse.org...
>I have a small application with a menu and a canvas wich displays real time
>data. (a new image is painted every 100ms. This repaint is
> made by a client thread).
> If I click on the menu bar and the menu is opened, the refresh stops!!
> It seems, that the readAndDispatch() call is blocking!
> How can I make this menu actions non-blocking?
>
> Michael
> ----------------- Code Snippet ---------------
> The following thread is started with display.asyncExec(new
> TestDataThread());
>
> public class TestDataThread extends Thread
> {
> public void run() {
> // Print new data
> try
> {
> while (display.readAndDispatch());
> Thread.sleep(100);
> } catch (InterruptedException e)
> { e.printStackTrace();
> }
> }
> }
>
Previous Topic:[No more handles] Exception on Win98 after changing to SWT 3.1
Next Topic:Refreshing View (Tableviewer)
Goto Forum:
  


Current Time: Thu Dec 18 23:56:42 GMT 2014

Powered by FUDForum. Page generated in 0.02723 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software