SWT exception: org. Eclipse. SWT. swtexception: Invalid thread access

Source: Internet
Author: User

Attempt to modify the interface element in a thread other than the thread that created the SWT interface will throw the following exception

Exception in thread "thread-0" org. Eclipse. SWT. swtexception: Invalid thread access

At org. Eclipse. SWT. SWT. Error (SWT. Java: 2942)

At org. Eclipse. SWT. SWT. Error (SWT. Java: 2865)

At org. Eclipse. SWT. SWT. Error (SWT. Java: 2836)

The preceding thread-0 is another enabled thread.


Analysis]:

In the SWT program,

SWT automatically creates a user interface thread

Non-User Interface threads cannot directly operate on User Interface threads

To try to modify the user interface in another thread, use the following method:

If (! This. display. isdisposed ()){

Runnable = new runnable (){

Public void run (){

// Modify the interface code

}

};

Display. syncexec (runnable); // The key lies in this sentence.

}

Description in SWT-DOC:

public void syncExec(java.lang.Runnable runnable)
Causes run()Method of the runnable to be invoked The user-interface threadAt the next reasonable opportunity. The thread which callthis method is suincluded until the runnable completes.
Parameters:
runnable-Code to run on the user-interface thread.

In addition, it corresponds to another method:

public void asyncExec(java.lang.Runnable runnable)
Causes run()Method of the runnable to be invoked The user-interface threadAt the next reasonable opportunity. The caller of this method continues to run in parallel, and is not notified when the runnable has completed.
Parameters:
runnable-Code to run on the user-interface thread.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.