Skip to main content



      Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » What must absolutely be inside the SWT thread
What must absolutely be inside the SWT thread [message #516683] Wed, 24 February 2010 13:41 Go to next message
Eclipse UserFriend
Hello,

I try to optimize a multithreaded application I develop inisde the eclipse framework.
I know that I may only access SWT elements from the SWT thread (f.e. by using asyncExec or syncExec).
I was not able to find a description what kinds of actions have to be inside the SWT thread and which can be somewhere else.
Normally any problematic action shall cause an SWTException "invalid thread access".
However I think I had done manipulations in the past that I think shouldn't be allowed but didn't cause an Exception.

Is it guaranteed that any invalid operation will cause the Exception?
Re: What must absolutely be inside the SWT thread [message #516695 is a reply to message #516683] Wed, 24 February 2010 14:54 Go to previous messageGo to next message
Eclipse UserFriend
Quote:
However I think I had done manipulations in the past that I think shouldn't be allowed but didn't cause an Exception.


for example....

[Updated on: Wed, 24 February 2010 14:55] by Moderator

Re: What must absolutely be inside the SWT thread [message #516938 is a reply to message #516683] Thu, 25 February 2010 09:44 Go to previous messageGo to next message
Eclipse UserFriend
Hello,

I already tried to find an example when writing the topic. I currently can't find one. I think it was connected to Text.setText, but I may have missed the fact that it was called from the SWT Thread.

The important question is:
Is every invalid action supposed to throw the exception and can I take the absence of an exception as a permission to do the action?
Re: What must absolutely be inside the SWT thread [message #517071 is a reply to message #516938] Thu, 25 February 2010 23:20 Go to previous messageGo to next message
Eclipse UserFriend
It all relates to Threads not actions....

What ever you do in SWT should be done from SWT's display thread...

If you notice in many methods of SWT controls there is a checkWidget method at the starting....

For example in text.setText method...
checkWidget method is there,if you go into that...

/**
 * Throws an <code>SWTException</code> if the receiver can not
 * be accessed by the caller. This may include both checks on
 * the state of the receiver and more generally on the entire
 * execution context. This method <em>should</em> be called by
 * widget implementors to enforce the standard SWT invariants.
 * <p>
 * Currently, it is an error to invoke any method (other than
 * <code>isDisposed()</code>) on a widget that has had its 
 * <code>dispose()</code> method called. It is also an error
 * to call widget methods from any thread that is different
 * from the thread that created the widget.
 * </p><p>
 * In future releases of SWT, there may be more or fewer error
 * checks and exceptions may be thrown for different reasons.
 * </p>
 *
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 */
protected void checkWidget () {
	Display display = this.display;
	if (display == null) error (SWT.ERROR_WIDGET_DISPOSED);
	if (display.thread != Thread.currentThread ()) {
		/*
		* Bug in IBM JVM 1.6.  For some reason, under
		* conditions that are yet to be full understood,
		* Thread.currentThread() is either returning null
		* or a different instance from the one that was
		* saved when the Display was created.  This is
		* possibly a JIT problem because modifying this
		* method to print logging information when the
		* error happens seems to fix the problem.  The
		* fix is to use operating system calls to verify
		* that the current thread is not the Display thread.
		* 
		* NOTE: Despite the fact that Thread.currentThread()
		* is used in other places, the failure has not been
		* observed in all places where it is called. 
		*/
		if (display.threadId != OS.GetCurrentThreadId ()) {
			error (SWT.ERROR_THREAD_INVALID_ACCESS);
		}
	}
	if ((state & DISPOSED) != 0) error (SWT.ERROR_WIDGET_DISPOSED);
}


you can see that there are checks for whether the current thread is a display thread or not...

This check is done in almost all methods of SWT classes...
Hence the exception invalid thread access....if you do some operation from any other thread(apart from display thread)...

This mechanism is many a times painful but They claim its efficient and less error prone(if somebody could explain it in detail pls) ...


Re: What must absolutely be inside the SWT thread [message #517202 is a reply to message #516938] Fri, 26 February 2010 10:17 Go to previous message
Eclipse UserFriend
> The important question is:
> Is every invalid action supposed to throw the exception and can I take the
absence of an exception as a permission to do the action?

Yes this is the intent. Most SWT API needs to be invoked on the main
thread, and will throw an exception otherwise. However SWT graphics
resources (types that subclass Resource like GC, Color, Font, etc.) can be
called from other threads without problem.

Grant
Previous Topic:Re: Retrieve cookies from introPart
Next Topic:MAC OS SWT Shell iconified/deiconified listener problem
Goto Forum:
  


Current Time: Mon Jul 14 05:21:33 EDT 2025

Powered by FUDForum. Page generated in 0.09283 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top