繁体   English   中英

在Eclipse RAP应用程序中执行后台操作

[英]Execute background action in Eclipse RAP application

我想在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();

有谁知道发生了什么事吗?

我发现。 应该在后台执行的代码不在正确的位置。 在我从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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM