How to troubleshoot cross-threading access control in Dotnet

Source: Internet
Author: User

How to troubleshoot cross-threading access control in Dotnet

Zhu Two (2008.10, reprint please keep the author)

If you are interested in this article, you may have encountered the following similar exception hints in programming:
1, the thread operation is invalid: it is accessed from a thread that is not creating the control "X". (x is the name of the control)
2. The operation performed on the control is being invoked from the wrong thread. Use Control.Invoke or Control.BeginInvoke to marshal to the correct thread to perform this exercise

For "

Seeing this hint is most likely caused by a control that is accessed across threads. For example, there is a textbox named Textboxa on the form forma.
The code is as follows: Private Sub forma_load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load D im th as New System.Threading.Thread (AddressOf crossthreadcalltest) th. Start () End Sub Public Sub Crossthreadcalltest () Textboxa.text = "ABC" End Sub

Briefly explain:
When the form is loaded, the thread th execution process is crossthreadcalltest and the Textboxa text is set inside the thread th.
Textboxa is created by the main thread, Chengri access on the th line, resulting in access to Windows controls across threads, and usually you get the first hint similar to the above.
The error message is as follows:
The operation performed on the control is being invoked from the wrong thread. Use Control.Invoke or Control.BeginInvoke to marshal to the correct thread to perform this operation.

Such problems are not encountered when using Visual Studio 2003, and in the. NET Framework 2.0, to avoid the same problem, you can use a simple

Single method, as long as you set the Checkforillegalcrossthreadcalls property of the TextBox class to False, the new code follows Private Sub forma_load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles mybase.load textbox.checkforillegalcrossthreadcalls = False D im th as New System.Threading.Thread (AddressOf crossthreadcalltest) th. Start () End Sub

But Checkforillegalcrossthreadcalls does not solve all the problems, such as the TreeView control is not so lucky, usually throws the 2nd hint above. And

Microsoft does not seem to recommend using Checkforillegalcrossthreadcalls for cross threading access to Windows controls, while recommending more secure access techniques.
The following example is an example of adding a node to the TreeView across Threads:    private sub forma_load (byval sender as  System.object, byval e as system.eventargs)  handles mybase.load          dim th as new system.threading.thread (AddressOf  Crossthreadcalltest)         th. Start ()     end sub     public sub crossthreadcalltest ()         dim deladd as new addnodedelegate ( Addressof addtreenode)         treeview1.begininvoke (DELADD)      end sub     private delegate sub addnodedelegate ()     private sub addtreenode ()          TREEVIEW1.NODES.ADD ("ABC")     End Sub

In this way, the Addtreenode process is still running in the main thread, of course, there will be no cross threading problems, a little change to the code can be explained:    private sub forma_load ( Byval sender as system.object, byval e as system.eventargs)  Handles  mybase.load         system.threading.thread.currentthread.name  =  "Main"         Dim th As New  System.Threading.Thread (addressof crossthreadcalltest)         th. name =  "th"         th. Start ()     end sub     public sub crossthreadcalltest ()         dim deladd as new addnodedelegate ( Addressof addtreenode)         treeview1.begininvoke (DELADD)      end sub     private&nbsP;delegate sub addnodedelegate ()     private sub addtreenode ()          treeview1.nodes.add ("ABC")           ' should be shown in main         messagebox.show ( System.Threading.Thread.CurrentThread.Name)     end sub

Just write so much and welcome the discussion in the form of comments.

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.