I ran in to an error today in WPF like this:
InvalidOperationException: Cannot perform this operation while dispatcher processing is suspended
After doing a little digging I found that this was happening because I was calling Dispatcher.PushFrame inside an event handler for TabItem.IsVisibleChanged. Looking at the MSDN documentation for Dispatcher.PushFrame I found that this exception will be raised if the dispatcher processing is disabled – and one of the times that WPF disables dispatcher processing is during the layout phase (has to do with preventing nested message loops, or something). It just so happened that this event was being raised while WPF was doing layout!
The solution? Use the Dispatcher to delay my event handler until the Dispatcher is available to handle PushFrame calls. So this:
tabItem.IsVisibleChanged += new DependencyPropertyChangedEventHandler( Tab_IsVisibleChanged );
Becomes this:
tabItem.IsVisibleChanged += (o, e) => Dispatcher.BeginInvoke( new DependencyPropertyChangedEventHandler( Tab_IsVisibleChanged ), o, e);
August 11, 2010 at 9:35 pm |
Awesome. This helped me out of a mess today, so thank you!
February 23, 2011 at 12:50 am |
Nice! Just the info I was looking for. Thank you for sharing