WPF同步动画和UI线程死锁

时间:2016-04-30 12:27:54

标签: wpf multithreading animation deadlock synchronous

我正在使用Viewport3D编写3D wpf应用程序。当用户按下按钮时,我必须在DoubleAnimation上启动AxisAngleRotation3D,但必须同步完成。我无法在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();

1 个答案:

答案 0 :(得分:1)

好的,这只是一个想法。为什么不用几步创建这个动画。首先启动第一批动画,然后,当他们完成另一组动画时。你可以将它与某种计时器同步。

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