WPF同步动画和UI线程死锁

Mat*_*usz 5 wpf animation multithreading deadlock synchronous

我正在使用Viewport3D编写3D wpf应用程序.当用户按下一个按钮,我必须开始DoubleAnimationAxisAngleRotation3D,但必须做到同步.我不能这样做animation.Completed,因为这个动画递归地运行下一个和下一个动画.

ButtonHandler必须在UI线程上工作,因为要计算动画我几乎不使用Viewport3D.

所以我必须在UI线程中启动同步动画并在完成后继续工作.

我尝试了这段代码,但它会导致死锁:

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();
Run Code Online (Sandbox Code Playgroud)

fra*_*iis 1

好吧,这只是一个想法。为什么不通过几个步骤来创建这个动画呢?首先启动第一批动画,然后当它们完成时运行另一组动画。您可以将其与某种计时器同步。

您可以首先创建要在每个步骤中运行的动画和对象列表,然后调用它们。