I am using Dispatcher.PushFrame to block my code while allowing the UI to refresh until a long running process is done. This works as expected, so long as my call to Dispatcher.PushFrame is from a button click event. If, however, I use this same code during the Page’s Loaded event or constructor, the UI does not refresh, and so never paints. As a random experiment, I tried using Window.ShowDialog from the constructor, and it does allow the UI to paint, even though control is blocked until the modal dialog closes. Can anyone offer a solution to allow this functionality from the Page Loaded event using Dispatcher.PushFrame or some other manual mechanism?
As an addendum, if I minimize or maximize my window, the UI paints and I can interact with it normally, but not until I manually perform the resize.
From my readings in MSDN on Object Lifetime Events and stumping around in Reflector it appears that the Loaded
and Unloaded
events are not raised in the same manner as other events. Internally a BroadcastEventHelper
class is used, which coordinates the various Loaded
events amongst every element in the visual tree before eventually raising them at the DispatcherPriority.Loaded
level.
I believe this is why you're seeing this behavior.
As for a concrete solution, I suggest long running tasks not be placed in the Page.Loaded
event handler and instead a BackgroundWorker
or Task
be issued to complete the work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With