readAndDispatch is blocking [message #464551] |
Thu, 24 November 2005 16:25 |
Eclipse User |
|
|
|
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 #464557 is a reply to message #464551] |
Thu, 24 November 2005 18:54 |
Veronika Irvine 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();
> }
> }
> }
>
|
|
|
Powered by
FUDForum. Page generated in 0.03653 seconds