隨後需要在一個新打開的窗口後啓動一個線程讀取一個對象並按秒刷新界面顯示。
仍然使用異步線程實現。
方法爲:
1、創建一個線程:
public class TestThread extends Thread{
public volatile boolean exit = false;
private Label label;
private Runnable runnable = new Runnable(){
public void run()
{
//注意if語句中的這句話,如果不加會在關閉窗口的時候出現Wedgit is disposed的異常
if(!label.isDisposed)
{
label.setText("test");
}
}
};
public void run()
{
while(!exit)
{
try {
sleep(1000);
} catch (Exception e) {
// TODO: handle exception
return;
}
display.asyncExec(runnable);
}
}
}
2、外部調用
TestThread vt = new TestThread ();
vt.setLabel(label);
vt.setDisplay(display);
vt.start();
3、關閉方法
shell.addShellListener(new ShellAdapter()
{
public void shellClosed(ShellEvent e) {
if(!vt.exit)
{
vt.exit = true;
try {
vt.join();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});