我想在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();
有没有人知道发生了什么?
答案 0 :(得分: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();