我做一个GUI的程序,基于eclipse swt,要10秒一次任务调度,要在GUI显示,可不可以这么用,是SWT线程模型导致的错误吗?
import org.eclipse.swt.widgets.Text;public class ClientFrame{
    private Text text=new ....;
    ................
    public void func(){
    text.append("hello");
    Timer timer=new Timer();
    TimerTask task=new MornitorData(text);
    timer.schedule(task,0,10000);
}
}other file:
public class MornitorData{
  private Text text=null;
  ...................
   public MornitorData(Text text){
     this.text=text;
     ...........
   }   public void run(){
      
   text.append("hello");//此处异常 exception in thread "Timer-0"
                                   org.eclipse.swt.SWTException:Invalid thread access
   }
}

解决方案 »

  1.   

    这个问题比较常见,swt有专门的例子讲述这个问题。
    import org.eclipse.swt.*;
    import org.eclipse.swt.widgets.*;public class ProgressorBar {public static void main (String [] args) {
    Display display = new Display ();
    Shell shell = new Shell (display);
    ProgressBar bar = new ProgressBar (shell, SWT.SMOOTH);
    bar.setBounds (10, 10, 200, 32);
    shell.open ();
    for (int i=0; i<=bar.getMaximum (); i++) {
    try {Thread.sleep (100);} catch (Throwable th) {}
    bar.setSelection (i);
    }
    while (!shell.isDisposed ()) {
    if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
    }