|
|
|
Re: What must absolutely be inside the SWT thread [message #517071 is a reply to message #516938] |
Fri, 26 February 2010 04:20 |
Vijay Raj Messages: 608 Registered: July 2009 |
Senior Member |
|
|
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) ...
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
|
|
|
|
Powered by
FUDForum. Page generated in 0.03240 seconds