Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Use of syncExec/asyncExec/timerExec - Threading issue - Code base
Use of syncExec/asyncExec/timerExec - Threading issue - Code base [message #207233] Mon, 08 March 2004 03:59 Go to next message
Eclipse UserFriend
Hello,

Some time ago, someone asked on this newsgroup how to use these methods
to have some easy interaction with the UI from non UI threads.
The problem was to stay in the same scope in the whole process (same
class, same instance, etc..) to keep access to some class attributes
needed by the process and set during the threaded part of the code.

The conversation went private by mail but the answer I finally provided
seemed to help.
Since this question comes back quite often, I thought it might help to
have a small code base for those to whom it is not perfectly clear even
after reading the developer guide in eclipse's help.
Of course, this code base is not final and any discussion is welcome to
make it more easier to understand for anyone.
I use a private member class to make it easier.

class Test {

private class AsyncAction implements Runnable {
/**
* @see java.lang.Runnable#run()
*/
public void run() {
Test.this.inUiMethod(); // This will call the inUiMethod()
method in the containing Test class instance.
}
}

private inUiMethod() {
// what needs to be done on the UI
}

public outOfUiMethod() {
...
Display.getDefault().asyncExec(new AsyncAction());
...
}
}

You can replace asyncExec by syncExec or timerExec.
- asyncExec will launch the run() method of the Runnable (AsyncAction)
asynchronously (when it wants, usually next time a redraw is called on
the display)
- syncExec will launch it when you want (on the call of syncExec)
- timerExec will launch it after the delay you have provided has elapsed.

The Display.getDefault() gets the default display and therefore gets the
UI thread in which the run() method of the Runnable will be executed.

You can get more information about thread issues in the eclipse help:
Menu Help -> Help Content
then Platform Plugin Developer Guide -> Programmer's Guide -> Standard
Widget Toolkit -> Threading Issues

Best regards,

Jean

--
"No brain, no headache"
Re: Use of syncExec/asyncExec/timerExec - Threading issue - Code base [message #207622 is a reply to message #207233] Tue, 09 March 2004 00:03 Go to previous message
Eclipse UserFriend
Hi Jean

Thanks for the explanation I have asked a similar question with "AsyncExec
Debugging Problem" heading.But one thing I don't have in my case is that
asyncExec method is used in JavaActionContributor class but this class
doesn't implement Runnable and I also don't get a compile time error. i
would appreciate any pointer.

Syed



Jean Couillaud wrote:

> Hello,

> Some time ago, someone asked on this newsgroup how to use these methods
> to have some easy interaction with the UI from non UI threads.
> The problem was to stay in the same scope in the whole process (same
> class, same instance, etc..) to keep access to some class attributes
> needed by the process and set during the threaded part of the code.

> The conversation went private by mail but the answer I finally provided
> seemed to help.
> Since this question comes back quite often, I thought it might help to
> have a small code base for those to whom it is not perfectly clear even
> after reading the developer guide in eclipse's help.
> Of course, this code base is not final and any discussion is welcome to
> make it more easier to understand for anyone.
> I use a private member class to make it easier.

> class Test {

> private class AsyncAction implements Runnable {
> /**
> * @see java.lang.Runnable#run()
> */
> public void run() {
> Test.this.inUiMethod(); // This will call the inUiMethod()
> method in the containing Test class instance.
> }
> }

> private inUiMethod() {
> // what needs to be done on the UI
> }

> public outOfUiMethod() {
> ...
> Display.getDefault().asyncExec(new AsyncAction());
> ...
> }
> }

> You can replace asyncExec by syncExec or timerExec.
> - asyncExec will launch the run() method of the Runnable (AsyncAction)
> asynchronously (when it wants, usually next time a redraw is called on
> the display)
> - syncExec will launch it when you want (on the call of syncExec)
> - timerExec will launch it after the delay you have provided has elapsed.

> The Display.getDefault() gets the default display and therefore gets the
> UI thread in which the run() method of the Runnable will be executed.

> You can get more information about thread issues in the eclipse help:
> Menu Help -> Help Content
> then Platform Plugin Developer Guide -> Programmer's Guide -> Standard
> Widget Toolkit -> Threading Issues

> Best regards,

> Jean
Previous Topic:help
Next Topic:How can I get WorkbenchWidnow's ActionBar
Goto Forum:
  


Current Time: Tue Jul 15 15:11:38 EDT 2025

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

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

Back to the top