WPF同步动画和UI线程死锁
WPF Synchronous animation and UI thread deadlock
我正在使用 Viewport3D 编写 3D wpf 应用程序。当用户按下按钮时,我必须在 AxisAngleRotation3D
上启动 DoubleAnimation
,但它必须同步完成。我在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();
好的,这只是一个想法。为什么不分几步创建这个动画。首先开始第一批动画,然后,当他们完成 运行 另一组动画时。您可以将它与某种计时器同步。
您可以先在每个步骤中为 运行 创建动画和对象列表,然后调用它们。
我正在使用 Viewport3D 编写 3D wpf 应用程序。当用户按下按钮时,我必须在 AxisAngleRotation3D
上启动 DoubleAnimation
,但它必须同步完成。我在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();
好的,这只是一个想法。为什么不分几步创建这个动画。首先开始第一批动画,然后,当他们完成 运行 另一组动画时。您可以将它与某种计时器同步。
您可以先在每个步骤中为 运行 创建动画和对象列表,然后调用它们。