WPF: What is the right way to wait for the storyboard animation to complete before performing the operation

I have a storyboard that I reuse to animate some images, I want to perform some operation after each animation that involves some calculations, and then start another animation, so I believe I should use the completed StoryBoard event MyStoryboard.Completed += storyboard_Com​pleted;

I am wondering if I should start the next animation in the current StoryBoard Storyboard_Completed Event ? And are there any consequences if I started the first animation in a separate thread using Object.Current.Dispatcher Object?

If I called StoryBoard.Begin () in a separate thread using Application.Current.Dispatcher, will the Storyboard_Completed event also be raised in the UI thread? In this case, do I still need to wrap the following animation in another dispatcher call?

 private void Story_Completed(object sender, EventArgs e) { Application.Current.Dispatcher.Invoke((Action)delegate() { SomeNewStoryBoardAnimation.Begin(); } } 

It is right? Or is there a better way to check if the storyboard is over and start the next set of calculations and animation of the storyboard right after that?

I was thinking about using one desktop background to handle all the animations and calculations in a sequence, but I'm also interested in how to “wait” for the animation to complete before starting the next set of calculations and animations. Is it OK for BackGroundWorker to have Thread.sleep while waiting for an animation?

+8
wpf wpf-controls
source share
2 answers

You can wrap the storyboard in a Task object and wait for it to complete.

Here is a great example code example illustrating how to do this, taken from a post

+7
source share

Using the Storyboard.Completed event should work for your purposes. The Storyboard.Completed event handler should trigger the UI thread, so you do not need to call Application.Current.Dispatcher.Invoke to disable the second storyboard.

There should be no consequences if you call the original Storyboard.Begin using Application.Current.Dispatcher.Invoke. This will not trigger the storyboard animation in the new stream. It will asynchronously invoke animation in the main user interface thread. If you call Begin on the user interface thread yourself or use Application.Current.Dispatcher.Invoke to do this, the end result should be the same. Your handler will end when the storyboard is over and you can complete your calculations and burn the next storyboard.

See the following question for some discussion of storyboards used in the past as a timer due to the fact that it works in the user interface thread:

What is the point of using storyboards as a timer?

Also, this is probably too large for the particular case you are describing, but if you need to arrange a sequence of sequential asynchronous operations, you can use Reactive Extensions:

http://msdn.microsoft.com/en-us/data/gg577609.aspx

The following article provides an example of a sequential storyboard (although the article is old enough that the syntax probably changed):

http://www.wintellect.com/cs/blogs/jlikness/archive/2010/08/22/coroutines-for-asynchronous-sequential-workflows-using-reactive-extensions-rx.aspx

+1
source share

All Articles