. Net multithreading and Windows Forms programming notes

Source: Internet
Author: User

FAQs about multi-threaded winform Programming

1. The UI thread is blocked and cannot respond to other messages in the form message queue.

2. Non-UI threads modify UI attributes because form resources are also critical resources, there is a mutex access mechanism.

3. thread synchronization problem thread a can only start execution after thread B completes execution.

Solution to problem 1:
Only one solution is to enable the new thread to execute time-consuming operations so that the original interface thread can still respond to user messages and system messages in the form message queue.

You can enable a new thread in the following ways:
1) Use the system. Threading. Thread class and the system. Threading. threadstart delegate or the system. Threading. parameterizedthreadstart delegate to enable the new thread.
Threadstart delegate type: void threadstart (void );
Type of the parameterizedthreadstart delegate: void parameterizedthreadstart (object []);

The threadstart delegate can point to a method without parameters and without return values.
The parameterizedthreadstart delegate can point to a method with parameters without return values.

When the Thread class is instantiated, You can input the instance entrusted by threadstart or the instance entrusted by parameterizedthreadstart to the constructor, and then call a method asynchronously using thread. Start.

2) define a delegate for the time-consuming method that requires asynchronous execution. Use the ininvoke method of the instance of the delegate to call this method asynchronously, the begininvoke method includes the asynccallback type callback function delegate and object type parameters.
Then, you can use the endinvoke method in the asynccallback type callback function to obtain the return value of the Asynchronous Method.

3) Use threadpool. queueuserworkitem (New waitcallback (method) to add a new thread to the thread pool

4) You can use system. timers. the timer Timer class is used to implement time-consuming operations in the new thread. timers. timer timer is different from system. windows. forms. timer timer, system. timers. the timer response function of the timer is not executed in the thread that calls the timer start method.

5) You can use the backgroundworker component to implement time-consuming operations in the new thread (by subscribing to dowork events ).

Solution to problem 2
BelowCodeIs the code in the. netframework 2.0 class library to avoid the critical resource deadlock problem caused by multi-threaded interface modification.
Internal implementation of system. Windows. Forms. Control. get_handle

Public Intptr get_handle ()
{
If (Checkforillegalcrossthreadcils &&   ! Incrossthreadsafecall) &&   This . Invokerequired)
{
Throw   New Invalidoperationexception (Sr. getstring ( " Illegalcrossthreadcall " , New   Object [] {This. Name} ));
}
If ( ! This . Ishandlecreated)
{
This. Createhandle ();
}
Return   This . Handleinternal;
}

When modifying the properties of each control, the get_handle method is called to obtain an operation handle, inside the method, the system checks the value of checkforillegalcrossthreadcils, a static member of the control class. (This member is used to indicate whether the security mode is enabled, the security mode prohibits cross-thread interface Attribute Modification to avoid multi-thread access to critical resource deadlocks ), the second property to be judged is the invokerequired property (which indicates whether the current method is called across threads ). Therefore, we can disable the security mode by modifying the checkforillegalcrossthreadcils attribute to false, but it may cause thread deadlock.

There are only two solutions
1) set the checkforillegalcrossthreadcils attribute to false, disable the security mode of. net, and add lock to the Code for modifying the interface property to implement interface Attribute Modification by only one thread at a time.
2) In the interface Property setting method, ask the invokerequired property. If the interface property is modified by a non-interface thread, ask the interface thread to call the interface Property setting method. (This method is the usual method in the msdn instance and is also the internal implementation of components such as backgroundworker)

We recommend that you use backgroundworker to implement time-consuming auxiliary threads and cross-thread interface Attribute Modification. For details about how to use backgroundworker, refer to the one-day training of backgroundworker.
Http://www.cnblogs.com/coderlee/archive/2007/12/27/1017620.html

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.