小编Mat*_*usz的帖子

WPF同步动画和UI线程死锁

我正在使用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)

wpf animation multithreading deadlock synchronous

5
推荐指数
1
解决办法
132
查看次数

标签 统计

animation ×1

deadlock ×1

multithreading ×1

synchronous ×1

wpf ×1