SWT多线程-关于非UI线程操作UI线程(一)

最近在使用SWT开发桌面程序。

在开发的过程中需要实现通过一个线程实时读取系统时间并能刷新界面上的时间显示。

 

使用如下代码实现:

1、创建一个Runnable

   //系统时间
   final Runnable timer = new Runnable()
   {
    public void run()
    {
     synchronized (this) {
      try {
         DateFormat d1 = DateFormat.getDateTimeInstance();          String str1 = d1.format(new Date());      
         shell.getStatusbarLabel().setText(str1);
      } catch (Exception e) {
       // TODO: handle exception
      }
     }
    }
   };

2、在代码中加入如下语句,其中timerExec为主线程中实现timer,并设定时间为主线城启动后0秒后执行。

   while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
     display.sleep();
    else    
      display.timerExec(0,timer);        
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章