C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » WPF »

WPF Dispatcher CheckAccess and VerifyAccess Mystery


Posted Date: 20 Jun 2009    Resource Type: Articles    Category: WPF
Author: ABitSmartMember Level: Diamond    
Rating: 1 out of 5Points: 15



This one really struck me hard. I am doing a threading scenario with some UI feedback marshaled back on the UI thread. And you won’t believe it, I am amazed to find CheckAccess missing from the Dispatcher object. Intellisense wouldn’t just show me CheckAccess(). Rubbing my eyes and again checking, it still seems to have vanished…

I had some test code which I had played with a while back using .Net 3.0 and with .Net 3.5 doing the same stuff using CheckAccess.The snippet from the test code,

public void Test()
{
Button theButton = button1 as Button;

if (theButton != null)
{
// Checking if this thread has access to the object.
if (theButton.Dispatcher.CheckAccess()) //<--- MYSTERY
{
// This thread has access so it can update the UI thread.
theButton.Content = "Hello";
}
else
{
// This thread does not have access to the UI thread.
// Place the update method on the Dispatcher of the UI thread.
theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
(Action)(() => { theButton.Content = "Hello"; }));
}
}
}

I might have been drunk then or definitely now. Is there no CheckAccess really ? How did the test code run then ? Did the thing even compile if there was no such method present ? Am I doing a typo ? No no no… So lets solve..eh..debug.. the mystery then. Wear the thinking hat!

First step, verify I was not drunk earlier while writing the test code. Run the test code. Compile and run. Wow! Excellent, everything working smoothly. So I am drunk now then eh? Copy the test code to the current project. Compile and run. Wow! Excellent, everything working fine. So I am drunk now. hmm should take care of myself more, perhaps.

But being inquisitive I wouldn’t stop yet. Let me see if it appears now in intellisense. Trying to get CheckAccess() using intellisense on the Dispatcher object still not working. Mystery not yet solved! The code compiles and runs as expected but why doesn’t the method show up then ??

Well, that’s because CheckAccess( and VerifyAccess) methods have been marked to not be Visible for intellisense. Wow! They are not supposed to be used by normal developers it seems! Microsoft reason

Mystery solved. One hour wasted. Happy programmer. Back to work.

Have fun.

For more details, visit http://abitsmart.com/?p=51



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
WPF VerifyAccess not visible  .  WPF CheckAccess not visible  .  WPF  .  VerifyAccess  .  Dispatcher  .  CheckAccess  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Previous Resource: WPF Combobox Databind to a Dataset
Return to Discussion Resource Index
Post New Resource
Category: WPF


Post resources and earn money!
 



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use