WPF Dispatcher CheckAccess not displaying in intellisense
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.
Tags: WPF
December 2nd, 2008 at 4:46 am
That is very interesting. Very funny.