Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » UI Thread question
UI Thread question [message #466185] Wed, 04 January 2006 13:19 Go to next message
Aleksandr Kravets is currently offline Aleksandr Kravets
Messages: 55
Registered: July 2009
Member
Hi,

I have an application that does database lookup and might take some time
to process the request. I tried to put the call to the database in a
thread, but screen still locks up. What am I doing wrong?

buttonOK.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
Display _display = Display.getCurrent();
_display.asyncExec (new Runnable () {
public void run () {
sRun = sRun.buildGUI(value,
clientName.toLowerCase());
if(sRun.isDataExists()){
shell.dispose();
}
}
});
}
});

And couple of questions:

1. What is the difference between asyncExec and syncExec
2. Good tutorial on SWT Threading.

thanks,
Alex
Re: UI Thread question [message #466187 is a reply to message #466185] Wed, 04 January 2006 13:28 Go to previous messageGo to next message
Eclipse User
Originally posted by: ingo.siebertNOSPAM.cas.de

Hi,

i'm trying to help but i'm also a beginner.

Both sync and async work on the UI thread.
Sync halts the execution on the current thread and asynch doens't halt
the current thread.

For a new thread, use the normal Java classes. If you need to call
methods from the UI thread, than choose sync or async.

Ingo

Aleksandr Kravets schrieb:
> Hi,
>
> I have an application that does database lookup and might take some time
> to process the request. I tried to put the call to the database in a
> thread, but screen still locks up. What am I doing wrong?
>
> buttonOK.addListener(SWT.Selection, new Listener() {
> public void handleEvent(Event event) {
> Display _display = Display.getCurrent();
> _display.asyncExec (new Runnable () {
> public void run () {
> sRun = sRun.buildGUI(value,
> clientName.toLowerCase());
> if(sRun.isDataExists()){
> shell.dispose();
> }
> }
> });
> }
> });
>
> And couple of questions:
>
> 1. What is the difference between asyncExec and syncExec
> 2. Good tutorial on SWT Threading.
>
> thanks,
> Alex
Re: UI Thread question [message #466220 is a reply to message #466187] Wed, 04 January 2006 19:02 Go to previous messageGo to next message
Lorenz Maierhofer is currently offline Lorenz Maierhofer
Messages: 88
Registered: July 2009
Member
Hi,

It's exactly as Ingo described:
asyncExec still makes your code run in the UI thread - and so during the
time it takes to execute it, your UI will be blocked.
As a rule of thumb, never do someting expensive in UI thread or in
async/syncExec. The better way is to write a real thread and update the
UI via async/syncExec.

About Threading:
Check out the Job API that eclipse provides (google will help you).
For standard Java threads, check out the Thread class.

Lorenz

Ingo Siebert wrote:
> Hi,
>
> i'm trying to help but i'm also a beginner.
>
> Both sync and async work on the UI thread.
> Sync halts the execution on the current thread and asynch doens't halt
> the current thread.
>
> For a new thread, use the normal Java classes. If you need to call
> methods from the UI thread, than choose sync or async.
>
> Ingo
>
> Aleksandr Kravets schrieb:
>> Hi,
>>
>> I have an application that does database lookup and might take some
>> time to process the request. I tried to put the call to the database
>> in a thread, but screen still locks up. What am I doing wrong?
>>
>> buttonOK.addListener(SWT.Selection, new Listener() {
>> public void handleEvent(Event event) {
>> Display _display = Display.getCurrent();
>> _display.asyncExec (new Runnable () {
>> public void run () {
>> sRun = sRun.buildGUI(value,
>> clientName.toLowerCase());
>> if(sRun.isDataExists()){
>> shell.dispose();
>> }
>> }
>> });
>> }
>> });
>>
>> And couple of questions:
>>
>> 1. What is the difference between asyncExec and syncExec
>> 2. Good tutorial on SWT Threading.
>>
>> thanks,
>> Alex
Re: UI Thread question [message #466231 is a reply to message #466185] Thu, 05 January 2006 01:35 Go to previous messageGo to next message
Vladimir Grishchenko is currently offline Vladimir Grishchenko
Messages: 76
Registered: July 2009
Member
As others pointed out runnables passed to syncExec and asyncExec are
executed on the UI thread. The main purpose for these methods is to update
the UI from a non-UI thread in synchronous (blocking) or asynchronous
(non-blocking) mode as any attempt to update the UI from a non-UI thread
will result in an exception.

The simplistic approach to unblock the UI is to launch a new thread in your
event handler and when the necessary data is fetched/constructed you update
the UI with it in a runnable passed to syncExec or asyncExec. You typically
have to make sure that no new background threads are created if there is one
already running.

I'd suggest to look into the jobs API which provides convenient mechanisms
for managing complexities typically found in multi-threaded applications.

--Vladimir.

"Aleksandr Kravets" <akravets@klgerweiss.com> wrote in message
news:dph3l8$moi$1@utils.eclipse.org...
> Hi,
>
> I have an application that does database lookup and might take some time
> to process the request. I tried to put the call to the database in a
> thread, but screen still locks up. What am I doing wrong?
>
> buttonOK.addListener(SWT.Selection, new Listener() {
> public void handleEvent(Event event) {
> Display _display = Display.getCurrent();
> _display.asyncExec (new Runnable () {
> public void run () {
> sRun = sRun.buildGUI(value,
> clientName.toLowerCase());
> if(sRun.isDataExists()){
> shell.dispose();
> }
> }
> });
> }
> });
>
> And couple of questions:
>
> 1. What is the difference between asyncExec and syncExec
> 2. Good tutorial on SWT Threading.
>
> thanks,
> Alex
Re: UI Thread question [message #466254 is a reply to message #466231] Thu, 05 January 2006 09:28 Go to previous messageGo to next message
Aleksandr Kravets is currently offline Aleksandr Kravets
Messages: 55
Registered: July 2009
Member
Thanks guys,

I played with these two methods in my code and see the difference
between the two.
Thanks for explanation!

Vladimir Grishchenko wrote:
> As others pointed out runnables passed to syncExec and asyncExec are
> executed on the UI thread. The main purpose for these methods is to update
> the UI from a non-UI thread in synchronous (blocking) or asynchronous
> (non-blocking) mode as any attempt to update the UI from a non-UI thread
> will result in an exception.
>
> The simplistic approach to unblock the UI is to launch a new thread in your
> event handler and when the necessary data is fetched/constructed you update
> the UI with it in a runnable passed to syncExec or asyncExec. You typically
> have to make sure that no new background threads are created if there is one
> already running.
>
> I'd suggest to look into the jobs API which provides convenient mechanisms
> for managing complexities typically found in multi-threaded applications.
>
> --Vladimir.
>
> "Aleksandr Kravets" <akravets@klgerweiss.com> wrote in message
> news:dph3l8$moi$1@utils.eclipse.org...
>
>>Hi,
>>
>>I have an application that does database lookup and might take some time
>>to process the request. I tried to put the call to the database in a
>>thread, but screen still locks up. What am I doing wrong?
>>
>> buttonOK.addListener(SWT.Selection, new Listener() {
>> public void handleEvent(Event event) {
>> Display _display = Display.getCurrent();
>> _display.asyncExec (new Runnable () {
>> public void run () {
>> sRun = sRun.buildGUI(value,
>>clientName.toLowerCase());
>> if(sRun.isDataExists()){
>> shell.dispose();
>> }
>> }
>> });
>> }
>> });
>>
>>And couple of questions:
>>
>>1. What is the difference between asyncExec and syncExec
>>2. Good tutorial on SWT Threading.
>>
>>thanks,
>>Alex
>
>
>
Re: UI Thread question [message #466258 is a reply to message #466185] Thu, 05 January 2006 09:31 Go to previous message
Veronika Irvine is currently offline Veronika Irvine
Messages: 1272
Registered: July 2009
Senior Member
See:
http://www.eclipse.org/swt/faq.php#uithread
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet7.java?rev=HEAD&am p;content-type=text/vnd.viewcvs-markup

"Aleksandr Kravets" <akravets@klgerweiss.com> wrote in message
news:dph3l8$moi$1@utils.eclipse.org...
> Hi,
>
> I have an application that does database lookup and might take some time
> to process the request. I tried to put the call to the database in a
> thread, but screen still locks up. What am I doing wrong?
>
> buttonOK.addListener(SWT.Selection, new Listener() {
> public void handleEvent(Event event) {
> Display _display = Display.getCurrent();
> _display.asyncExec (new Runnable () {
> public void run () {
> sRun = sRun.buildGUI(value,
> clientName.toLowerCase());
> if(sRun.isDataExists()){
> shell.dispose();
> }
> }
> });
> }
> });
>
> And couple of questions:
>
> 1. What is the difference between asyncExec and syncExec
> 2. Good tutorial on SWT Threading.
>
> thanks,
> Alex
Previous Topic:How to combine different images to one image?
Next Topic:Adding an Action to the Navigator View
Goto Forum:
  


Current Time: Thu Aug 28 03:14:00 EDT 2014

Powered by FUDForum. Page generated in 0.01774 seconds