A Brief Introduction to cross-thread access control in. Net3.0,. net3.0

Source: Internet
Author: User

A Brief Introduction to cross-thread access control in. Net3.0,. net3.0

During the past two days, when using WPF as the UI part of a project, it was found that the UI control was accessed across threads, and an exception was reported naturally. At that time, I did not find the InvokeRequired attribute and the Invoke method in the control for a long time. I was very depressed and found that in. net3.0, this changed.

The method used to replace InvokeRequired is the DispatcherObject. CheckAccess () or DispatcherObject. VerifyAccess () method, which indicates whether the current thread can directly access the control.

The method to replace Invoke is the DispatcherObject. Dispatcher. BeginInvoke (...) method.

Reference code:

 

// Uses the DispatcherObject. CheckAccess method to determine if
// The calling thread has access to the thread the UI object is on
Private void TryToUpdateButtonCheckAccess (object uiObject)
{
Button theButton = uiObject as Button;

If (theButton! = Null)
{
// Checking if this thread has access to the object
If (theButton. CheckAccess ())
{
// This thread has access so it can update the UI thread
UpdateButtonUI (theButton );
}
Else
{
// This thread does not have access to the UI thread
// Pushing update method on the Dispatcher of the UI thread
TheButton. Dispatcher. BeginInvoke (DispatcherPriority. Normal,
New UpdateUIDelegate (UpdateButtonUI), theButton );
}
}
} Reprinted from: http://www.aspnetjia.com/Cont-172.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.