Cross-thread access ------ reprint

Source: Internet
Author: User

There can be two methods:
One is by setting the system. windows. forms. control. checkforillegalcrossthreadcils = false; (in winform) If this attribute is set during program initialization, and all the controls in your control are controls in the Microsoft framework class library, the system will not throw the error you mentioned above. Of course, this is only a common method used to convert vs2003 code to vs2005. It is not recommended.

The second method is a common method recommended by Microsoft for cross-thread calling. It is implemented by a proxy, that is, put the code you want to operate on into a proxy, then, the proxy is handed over to the thread that creates the control to execute your code. For example:
Private void form1_load (Object sender, eventargs E)
{
// The system. Threading namespace is required to create a thread.
Thread T1, T2; // The description is a form class member.
T1 = new thread (New threadstart (backgroundprocess ));
T1.start (); // start thread T1
}
/// <Summary>
/// Define a proxy
/// </Summary>
Private delegate void dd ();

Private void backgroundprocess ()
{
// Instantiate the proxy as an anonymous proxy
Dd = delegate ()
{
Int I = 1;
While (true)
{
// Add a project to the list box
Listbox1.items. Add ("iterations:" + I. tostring ());

I ++;
Thread. Sleep (2000); // specifies the thread sleep time
}
};
Listbox1.invoke (dd );
}
The above Code only declares a proxy in your code and uses the newly added syntax (anonymous proxy, also known as anonymous method) in vs2005 .) In this example, the proxy puts all the code you want to operate in the thread into this anonymous method. Then, call this proxy by using the control's invoke method (you can also use the begininvoke method of the control -- The invoke method is synchronous and the begininvoke method is asynchronous. Of course, you can also enter any form of proxy in the invoke method. The Code called in this way will not throw the exception you encountered.

The above proxy method is similar to the callback function in C ++. You have written the execution method and then notified a thread to call your method by that thread, in this way, the internal component of the thread can be modified in a fixed thread. This completely achieves thread security.

Certificate ------------------------------------------------------------------------------------------------------------------------------------------------

Try ..

// Define a mutex for mutex access to ListBox
Mutex MX = new mutex ();
Public Delegate void myinvoke (string Str );
Private void updatelistbox (string Str)
{
// Update
MX. waitone ();
This. listbox1.items. Add (STR );
MX. releasemutex ();
}
Private void button#click (Object sender, eventargs E)
{
Thread onethread = new thread (New threadstart (this. run1 ));
Onethread. Name = "one ";
Thread towthread = new thread (New threadstart (this. run2 ));
Towthread. Name = "tow ";
Onethread. Start ();
Towthread. Start ();
}
Private void run1 ()
{
For (INT I = 0; I <100; I ++)
{

Myinvoke MI = new myinvoke (updatelistbox );
This. begininvoke (MI, new object [] {thread. currentthread. Name + I. tostring ()});
}
}
Private void run2 ()
{
For (INT I = 0; I>-100; I --)
{
Myinvoke MI = new myinvoke (updatelistbox );
This. begininvoke (MI, new object [] {thread. currentthread. Name + I. tostring ()});
}
}

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.