繁体   English   中英

更新文本字段时无效的线程访问异常

[英]Invalid thread access exception while updating textfield

我正在尝试使用可运行线程来更新属于另一个类的文本字段,但出现“无效线程访问”异常。

我的代码是:

 Thread t2 = new Thread () {

    public void run () {
        System.out.println("t2 thread içindeyim.");
        try {
            String sql =  " RESTORE DATABASE Genius3"+
                          " FROM DISK = '"+collected.getdbpath()+"'"+
                          " WITH MOVE 'GeniusIII_Data' TO 'C:\\SQLDATA\\Genius3.mdf',"+
                          " MOVE 'GeniusIII_Log' TO 'C:\\SQLDATA\\Genius3_1.ldf'";

            Class.forName(driver).newInstance();
            con = DriverManager.getConnection(url);
            stmt = con.prepareStatement(sql);
            rs = stmt.executeQuery();}
            catch (Exception ef) {ef.printStackTrace();}
            finally {

           ekran5.text_1.setText("done");

           }        
    }
};

另一个有那个文本框的类;

 public class ekran5 {

public static Label islemlabel;

public static Composite composite_1 ;
public static Label detail;
 public static  Text text_1;
public static void start(){


composite_1 = new Composite(Loader.composite, SWT.BORDER | SWT.EMBEDDED);
composite_1.setBackground(SWTResourceManager.getColor(192,192,192));
composite_1.setBounds(362, 83, 668, 536);

islemlabel = new Label(composite_1, SWT.NONE);
islemlabel.setFont(SWTResourceManager.getFont("Arial CYR", 10, SWT.NORMAL));
islemlabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
islemlabel.setBounds(10, 448, 84, 15);
islemlabel.setText("\u0130\u015Flem Detay\u0131");

text_1 = new Text(composite_1, SWT.BORDER);
text_1.setBounds(10, 469, 628, 37);
}

}

在finally块中,我正在更新文本字段,但是由于这个原因,它给了我一个无效的线程访问异常。

在SWT中,仅允许UI线程更新小部件,请尝试使用Display.syncExec来执行所有UI更新。 (或者,可以使用UI作业。)

阅读SWT常见问题解答也是明智之举,对我来说,这听起来像是常见问题解答。

暂无
暂无

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

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