0

I want my app to animate textblock one by one. When one animation is finished the next should start. When this line if (hasEnded == true | x==0) is commented it works (but its whole text not single line), if not application freezes. This is my code.

private void Animatebtn_Click(object sender, RoutedEventArgs e)
    {

        if (saccades != null)
        {
            LinearGradientBrush colorBrush = LoadBrush();

            for (int x = 0; x < textArray.Length;)
            {
                if (hasEnded == true | x==0)
                {
                    var temp = (TextBlock)this.FindName("tb" + x);
                    mytb = temp;
                    mytb.Foreground = colorBrush;

                    sb = Animate(sbduration, aduration, abegintime);
                    sb.Completed += new EventHandler(Story_Completed);
                    sb.Begin(this); // starts the animation
                    hasEnded = false;
                    x++;
                }
            }
        }
    private void Story_Completed(object sender, EventArgs e)
    {
        hasEnded = true;
    }

And this is my animation:

    public Storyboard Animate(double sBoardduration, double animationDuration,  double animationBeginTime)
    {

        Duration storybDuration = TimeSpan.FromSeconds(sBoardduration);
        TimeSpan animBegintime = TimeSpan.FromSeconds(animationBeginTime);
        Duration animDuration = TimeSpan.FromSeconds(animationDuration);

        Storyboard sb = new Storyboard() { Duration = storybDuration}; 
        DoubleAnimation DA = new DoubleAnimation() 
        { By = 1, Duration = animDuration, BeginTime = animBegintime };
        DoubleAnimation DA2 = new DoubleAnimation()
        { By = 1, Duration = animDuration, BeginTime = animBegintime };
        sb.Children.Add(DA); sb.Children.Add(DA2);
        Storyboard.SetTargetName(DA, "redStop");
        Storyboard.SetTargetName(DA2, "BlackStop");
        Storyboard.SetTargetProperty(DA, new PropertyPath("Offset"));
        Storyboard.SetTargetProperty(DA2, new PropertyPath("Offset"));
        return sb;
    }
LGrabny
  • 11
  • 3
  • 1
    You can't run storyboards on a thread other than the UI thread. – Ron Beyer Dec 21 '19 at 17:32
  • Good to know. Do have any idea why it doesn't work? – LGrabny Dec 21 '19 at 17:56
  • you may check this https://stackoverflow.com/questions/34038088/wpf-starting-animation-after-another-animation-is-finished-in-xaml link as well. Maybe it can give you a clue. – Rafaqat Ali Dec 21 '19 at 21:02
  • @Rafaqat In my case Story_Completed function never fires – LGrabny Dec 21 '19 at 21:06
  • This doesn't work. First of all you are using a binary OR but are supposed to use a logical OR. So `|` must become `||`: `hasEnded == true || x==0`. – BionicCode Dec 23 '19 at 16:56
  • Second faw is in your flow. Your are starting the animation. Execution is synchronized which means the `for` loop will "suspend" until the animation has completed. Then the `Completed` event is called which sets the `hasEnded` flag to `true`. Then `for` loop execution continues and sets the flag `hasEnded` to false again. Now you increment `x` and the loop enters the next iteration. But since `hasEnded` is `false` and `x` > 0 you will never enter the if-block again. – BionicCode Dec 23 '19 at 16:57
  • Therefore `x` will never increment and the termination condition of the for-loop will/can _never_ return `false` -> infinite loop -> UI busy infinitely -> UI freezes since there are no resources left for rendering and other tasks. – BionicCode Dec 23 '19 at 16:57
  • Solution: remove the `hasEnded` flag and the conditional checks as well as the event handler for the `Completed` event. just the plain for-loop. – BionicCode Dec 23 '19 at 17:02
  • Also if you execute the animations independently you don't need to add the animations to a `Storyboard`. You can get rid of this overhead by just invoking [`UIElement.BeginAnimation()`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.uielement.beginanimation?view=netframework-4.8) and pass in the target property and the animation instance. – BionicCode Dec 23 '19 at 17:06

0 Answers0