Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Synchronous animation and UI thread deadlock

I'm writing a 3D wpf application using Viewport3D. When user push a button, I must start DoubleAnimation on AxisAngleRotation3D, but it must be done synchronous. I can't do it on animation.Completed, because this animation run the next one and the next one recursively.

ButtonHandler must work on UI thread, because to calculate animation I hardly use Viewport3D.

So I must in UI thread start synchronous animation and after it finished continoue work.

I tried this code, but it cause deadlock:

AxisAngleRotation3D axis;
DoubleAnimation animation;
DependencyProperty dp;

var worker = new Thread(() =>
{
      Dispatcher.CurrentDispatcher.InvokeShutdown();
      Dispatcher.Run(); 
});

worker.SetApartmentState(ApartmentState.STA);
worker.Start();

AutoResetEvent animationDone = new AutoResetEvent(false);
EventHandler handler = null;
handler = (sender, args) =>
{
     animation.Completed -= handler; // remove self 
     animationDone.Set(); // signal completion 
};

animation.Completed += handler;

Dispatcher.CurrentDispatcher.Invoke(new Action(() =>
{
      axis.BeginAnimation(dp, animation);
}));

animationDone.WaitOne();
like image 273
Mateusz Avatar asked Nov 09 '22 15:11

Mateusz


1 Answers

Ok, it's just an idea. Why don't you create this animations in few steps. First start first batch of animation and then, when they'll finish run another set of animations. You could synchronize it with some kind of timer.

You can first create list of animations and objects to run in each step and then just invoke them.

like image 188
franiis Avatar answered Nov 15 '22 07:11

franiis