1

I want to execute a query in background in my Eclipse RAP application, but without blocking the UI. I followed this guide: http://eclipse.org/rap/developers-guide/devguide.php?topic=threads.html&version=2.2

But without success :/

The query is executed, but it always blocks the UI when called. Here's the code I'm using:

                final ServerPushSession pushSession = new ServerPushSession();

                Runnable bgRunnable = new Runnable() {

                   public void run() {

                     // schedule the UI update
                     display.syncExec( new Runnable() {

                         public void run() {

                                try {

                                    //CODE

                                    try {
                                        Thread.sleep(5000);
                                    }
                                    catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                                catch (PartInitException e) {

                                    e.printStackTrace();
                                    System.exit(0);
                                }

                                setText( "updated" );
                       }
                     } );

                     // close push session when finished
                     pushSession.stop();
                   }
                 };

                 pushSession.start();
                 Thread bgThread = new Thread( bgRunnable );
                 bgThread.setDaemon( true );
                 bgThread.start();

Does anyone have an idea about what's happening?

ccoutinho
  • 3,308
  • 5
  • 39
  • 47
  • Ugh.. from what I know, RAP applications run everything in a single UI thread, since it's inside the browser. It would be nice to see an actual answer to this, though. – Georgian May 02 '14 at 12:02
  • 1
    I haven't tested your code but since you force a sync execution while waiting 5 sec on the bgThread I tend to say the UI thread is forced to wait, too. Since sync forces the target thread not to return to anything else unless the *most recent sync* is fullfilled otherwise no save state could be guaranteed. So in turn try display.asyncExec – dkeck May 03 '14 at 00:27
  • @dkeck thank you for your answer! I have tried both without success already... – ccoutinho May 06 '14 at 07:50
  • 1
    @GGrec I'm pretty sure it's possible, and I'll have to make this work. So as soon as I figure it out, I will answer this! – ccoutinho May 06 '14 at 07:53

1 Answers1

1

I found out. The code which is supposed to be executed in background was not in the right place. It worked right after I removed the Thread.sleep from inside the display.syncExec, and I didn't even had to replace syncExec method for the asyncExec method. Below you can find a code sample, and your code might work if you place it below the "CODE" comment.

                final ServerPushSession pushSession = new ServerPushSession();

            Runnable bgRunnable = new Runnable() {

               public void run() {

                            try {


                                //CODE

                                try {
                                    Thread.sleep(5000);
                                }
                                catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                            }
                            catch (PartInitException e) {

                                e.printStackTrace();
                                System.exit(0);
                            }

                 // schedule the UI update
                 display.syncExec( new Runnable() {

                     public void run() {

                            setText( "updated" );
                   }
                 } );

                 // close push session when finished
                 pushSession.stop();
               }
             };

             pushSession.start();
             Thread bgThread = new Thread( bgRunnable );
             bgThread.setDaemon( true );
             bgThread.start();
ccoutinho
  • 3,308
  • 5
  • 39
  • 47