40027

SWT/Swing -> Threads n' Hell

I have an SWT application. I was frustrated at SWT for not making it easy to do what I wanted to do. So I used Swing. Swing made my life easy. Yay. Except now, the two have to talk. The issue is, they are running on different threads, and whenever I try to run an SWT GUI method from Swing, I get:

Exception in thread "AWT-EventQueue-0" org.eclipse.swt.SWTException: Invalid thread access

Oh joy. So, how do I fix this? If this was Objective-C, I could use:

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait

But it's not, so I can't. Is there no way to run method A on thread B? When that thread is existing?

Answer1:

I haven't worked much with SWT but it seems that SWT will striclty prohibit accessing an SWT object in the non UI thread. You can queue up a request to talk to an object on the SWT.

This link explains this better

The example suggests you to use syncExec

display.syncExec( new Runnable() { public void run(){ label.setText(text); } });

This I can assume will queue up the Runnable on the main SWT UI thread and thus the access would be legal.

Note: I read over the link again, syncExec will end up freezing your Swing EDT. If you invoke syncExec, it will throw the Runnable on the SWT queue and wait for it to be completed. You can look at asyncEvent to throw the runnable on the queue and continue on working.

Recommend

  • Eclipse CDT error: “Symbol 'XXX' could not be resolved”
  • Get all URLs for resources in sub-directory in Swift
  • How can I get the GUID from a PDB file?
  • Error in SWT/LogCat? or Android 2.2 AVD
  • Display some part of text in bold and some part in italic in SWT label
  • Is it possible to resolve my ClaimsAuthorizationManager using a DI container
  • How to read JSON-LD data from HTML in Objective-C?
  • JavaScriptCore External Arrays
  • Wrapping text in spinner android
  • Any nice way to generate a timeline view of commits from subversion?
  • Local Development, Apache vs Developer - file permissions
  • CakePHP ACL tutorial initDB function warnings
  • Problems installing Yesod for Haskell
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • ImageMagick, replace semi-transparent white with opaque white
  • Using variable in a value field in jMeter
  • Cannot connect to cassandra from Spark
  • How do I change content of ComboFieldEditor?
  • JFileChooser in front of fullscreen Swing application
  • HTML download movie download link
  • Bug in WPF DataGrid
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • TFS: Get latest causes slow project reloading
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Cross-Platform Protobuf Serialization
  • Javascript Callbacks with Object constructor
  • sending/ receiving email in Java
  • Eraser for UIBezierPath
  • How to make Safari send if-modified-since header?
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • QuartzCore.framework for Mono Develop
  • Numpy divide by zero. Why?
  • How to get icons for entities from eclipse?
  • Suggestions to manage Login/Logout transitions
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?