Andy
Andy

Reputation: 1043

Java: Invalid thread access

I am java noob programmer and I have got problem with SWT Exception Invalid thread access, I have look for that and I have found that It happens when you try to act upon an interface item from a thread that's not the UI thread. but I don't know how to fix it.

Problem: I have got 4 classes:

Problem is that it crash with Invalid thread access. I have no idea how to show two swt windows (completely different classes)


That application downloads webpage, parse it and get information I want. Then using class notify it shows notification. I added class Window that is SWT Window and it shows tray icon, there are settings it also is main class and that class run ZadaneReader thread. First question: should I add SWT window (main goal of that is: tray icon and settings) in other way?

My application contains following classes:

At the moment: it works when I am not using UniNotifyWindow or I am not using Window (start Zadane thread with tester class).

I suppose it is noobish question and I have made many mistakes, but I wish you won't blame me but I would be pleased if you could tell me what I am doing wrong.

Source: https://docs.google.com/open?id=0B5VL4J_7HrgaTkt4a1Q2VVQxZEE

Upvotes: 0

Views: 473

Answers (1)

Eugene
Eugene

Reputation: 9474

You may only access SWT UI elements from the main (aka "display") thread. This means that you can't do as much as set text in a text box from another thread. The easiest way to push work to display thread is by calling

Display.getDefault().asyncExec(...)

Be warned that you should be discrete in what you run in your display thread - long-running tasks will make your UI unresponsive.

Upvotes: 2

Related Questions