2014-05-02 78 views
1

我想在我的Eclipse RAP應用程序的後臺執行查詢,但不會阻塞UI。我遵循這個指南:http://eclipse.org/rap/developers-guide/devguide.php?topic=threads.html&version=2.2在Eclipse RAP應用程序中執行後臺操作

但沒有成功:/

執行查詢,但它總是塊調用時的UI。這裏是我使用的代碼:

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

有沒有人有關於發生了什麼的想法?

+0

呃..據我所知,RAP應用程序在單個UI線程中運行所有內容,因爲它在瀏覽器內。不過,看到實際的答案會很高興。 – GGrec

+1

我還沒有測試過你的代碼,但是因爲你在bgThread上等了5秒時強制執行同步操作,所以我傾向於說UI線程也被迫等待。由於同步強制目標線程不會返回其他任何內容,除非最新的同步*被填滿,否則不能保證保存狀態。所以依次嘗試display.asyncExec – dkeck

+0

@dkeck謝謝你的回答!我已經嘗試都沒有成功已經... – rsy

回答

1

我發現了。應該在後臺執行的代碼不在正確的位置。它在我從display.syncExec中刪除Thread.sleep後正常工作,我甚至不必爲asyncExec方法替換syncExec方法。在下面你可以找到一個代碼示例,如果你把它放在「CODE」註釋下面,你的代碼可能會工作。

   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(); 
+0

我想你不應該在你的webapp中真的調用'System.exit(0)'... – alphakermit

相關問題