Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] AsyncExec() problem ?

Hi Sylvain,

Yes it seems that it s in only a funcion call. that s why the the "infinite loop" stuck.
for me, this behaviour should be ok for "syncExec"
but the doc say for AsyncExec:
"The caller of this method continues to run in parallel"
which can be bad interpreted.

Maybe you misunderstood the basic concept. Suppose, you have a
background thread that wants to update the GUI. You are not
allowed to call the (some) methods of SWT from another
thread than the GUI thread. The solution is to pack the code
that calls SWT into a runnable and put it into
the SWT event queue. There are two ways of doing this:

- syncExec: the calling thread blocks until the runnable
  has finished to run in the SWT thread. The control
  flow in your thread is the same as if you would be in
  the SWT thread: the next statemnt in your code is called
  after the GUI has updated

- asyncExec: here the runnable is put into the SWT event queue
  and the call returns immediately. The runnable has (most probably)
  not yet been executed, when the call returns. But your background
  thread can continue working here. The GUI update happens in
  "parallel" at some later time.

Considering this, the docu of asyncExec is absolutely correct:
  "The caller of this method continues to run in parallel"

Here is nothing that can be misunderstood, unless you are not
familar with thread programming...

In general the Runnables for (a)syncExec should take very short
time. Else they block the GUI. If you have a calculation loop,
that takes long, and you want to update the GUI from time
to time, run the loop in a background thread.

But be aware: thread programming is not simple! Read a good
tutorial or book on thread programming. It took me quite a
while to understand thread programming. I have been doing
single thread GUI programming for over 10 years before I
started getting into threaded programming. Many of the rules
and paradigms I knew from single threaded programming are
not applicable in an multi threaded environment. It took
me months until I could say, I understand the basic principle
and problems. And still: I don't feel confident to create even
a simple robust and correct threaded application using the low
level java threading mechanisms. There are some libraries
(http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html)
that can help you.

Another approach is using one principle:
  All inter-thread communication has to be done in the SWT
  event thread using Runnables and (a)syncExec.

With this simple principle you can create pretty robust threaded
applications with no danger of deadlock or other
threading problems. However, it might not be the most
efficient way of doing multy threading....

Michael




Back to the top