1

我想在我的 Eclipse RAP 应用程序的后台执行查询,但不会阻塞 UI。我遵循了本指南:http ://eclipse.org/rap/developers-guide/devguide.php?topic=threads.html&version=2.2

但没有成功:/

查询已执行,但在调用时它总是阻塞 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();

有人知道发生了什么吗?

4

1 回答 1

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();
于 2014-05-06T09:21:18.683 回答