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.
我正在使用Viewport3D编写3D wpf应用程序。当用户按下按钮时,我必须在AxisAngleRotation3D上启动DoubleAnimation,但必须同步完成。我无法在动画上完成。完成,因为这个动画递归地运行下一个和下一个动画。
ButtonHandler must work on UI thread, because to calculate animation I hardly use Viewport3D.
ButtonHandler必须在UI线程上工作,因为要计算动画我几乎不使用Viewport3D。
So I must in UI thread start synchronous animation and after it finished continoue work.
所以我必须在UI线程中启动同步动画并在完成后继续工作。
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();
1 个解决方案
#1
1
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.
您可以先创建要在每个步骤中运行的动画和对象列表,然后只需调用它们。
#1
1
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.
您可以先创建要在每个步骤中运行的动画和对象列表,然后只需调用它们。