0

I'm trying to update a textfield which belongs to another class using a runnable thread, but I'm getting 'invalid thread access' exception.

My code is:

 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");

           }        
    }
};

and the other class which has that textfield in it ;

 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);
}

}

In the finally block, I'm updating the textfield but it gives me an invalid thread access exception because of this.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Yasin Bilir
  • 194
  • 3
  • 17
  • Is your application WPF? What is the exact error? – jtimperley Mar 12 '15 at 22:45
  • If you are getting an exception related to threads, I think it is unlikely that your question relates to sql server... you might get better help if you tag your question appropriately? – G B Mar 12 '15 at 23:47
  • Ya, just realised the tag problem, :) – Yasin Bilir Mar 13 '15 at 04:01
  • 1
    possible duplicate of [Invalid Thread Access Error with Java SWT](http://stackoverflow.com/questions/5980316/invalid-thread-access-error-with-java-swt) – Baz Mar 13 '15 at 08:56

1 Answers1

1

In SWT only the UI thread is allowed to update widgets try using Display.syncExec to perform all the UI updates. (Alternatively, a UI job can be used.)

Also reading the SWT FAQ would be a wise move, this sounds like a FAQ to me.

Alex Fitzpatrick
  • 643
  • 5
  • 10
  • I tried to use that but i dont have a display in the class that im runnning the thread. Only the main class has a display, im adding textfield on it from another class, im kind of new on this and i couldnt get success on putting the phraze that you gave. Should i put it after the display block that has been described in the main class ? – Yasin Bilir Mar 13 '15 at 05:10
  • Display.getDefault should give you the current display. If not (I'm not in front of a computer to check) you should make sure your code can access the display. – Alex Fitzpatrick Mar 13 '15 at 05:17