Search code examples
eclipsemultithreadingeclipse-rap

Execute background action in Eclipse RAP application


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?


Solution

  • 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();