I was trying to animate some stuff in WPF
and run some other operations when animation finishes.
Also, wanted to avoid animation finish callback mechanism, so, I came up with a solution illustrated in the code below:
// Start one second of animation
...
// Pause for one second
Wait(this.Dispatcher, 1000);
// Continue and do some other stuff
...
Now, the interesting part is Wait
method which magically makes blocking pause in my code but the animation and UI stays normal, responsive:
public static void Wait(Dispatcher Dispatcher, int Milliseconds)
{
var Frame = new DispatcherFrame();
ThreadPool.QueueUserWorkItem(State =>
{
Thread.Sleep(Milliseconds);
Frame.Continue = false;
});
Dispatcher.PushFrame(Frame);
}
I have read a documentation and a few articles about DispatcherFrame
but I am still unable to figure out what is really happening under the hood, and I need some clarification about how this construction with PushFrame
really works.