Click here to Skip to main content
14,694,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have a function called fadeTo() which changes an opacity with an animation. i want to know how can i wait until the storyboard and animation in fadeTo are finished then continue the rest of my program.

for example if i hit on button i want image1 to fade out and then make a new image. but what happens is it just makes a new image before the fade time so there will be no effect:

private void button_Click(object sender, RoutedEventArgs e)
{
    //fadeTo(Control, OpacityToFade, Duration);
    fadeTo(image1, 0.0, 500);
    
    //I want the program to wait here till the fade finishes but i dont know how to?
    image1 = new Image();
    .
    .
    .
}


How can i do it?

thanks
Posted
Updated 3-Dec-11 20:51pm
v3
Comments
[no name] 3-Dec-11 21:08pm
   
EDIT: added code block

For performing tasks when an animation is finished, you can use the Completed event of the animation.


If you don't have the animation class but you have the duration of the animation (like in your case), you can use a Timer with that duration.


I don't suggest to use the Thread.Sleep method here, because we don't want to block the UI...

   
Comments
M-Shaf 3-Dec-11 13:57pm
   
actually i don't want to do a task after the animation is finished. i want to stop the program and wait until the animation is finished. like the example i wrote
Shmuel Zang 4-Dec-11 2:48am
   
As it looks, you do want to perform a task after the animation is finished. The task is: "make a new image". So, if you want to hold the algorithm until the animation is finished, you can use the Completed event or a Timer, like I wrote.
If you want to disable some UI elements until the animation is finished, you can use the IsEnabled property of the elements.
If you realy want to block the UI (not recomended), you can use the Thread.Sleep method with the duration of the animation.
RaviRanjanKr 4-Dec-11 3:12am
   
My 5+
Shmuel Zang 4-Dec-11 4:10am
   
Thank you Ravi.
RaviRanjanKr 4-Dec-11 5:53am
   
Most welcome :)

In addition to my first solution, I just remembered that I've already posted a tip for solving this issue, two months ago (How could I forget it... :) ).


You can try to use my SuspendedButton. In the BeforeClick event-handler, start the animation and, in the Click event-handler, perform the task that has to be done after the animation is finished.

   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900