Code sometimes does not run. ??!

Below you will find a function I have to update a TextBlock. It fades out that textblock, changes the value and then fades it back in. Simply right? Yea, most the time it works find. Sometimes it does not, rarely but sometimes.

public static void TextChange(TextBlock UITextBlock, string Text) { try { Console.WriteLine("Text Change 1: " + Text); Storyboard sbFadeOut = new Storyboard(); DoubleAnimation animFadeOut = new DoubleAnimation(); animFadeOut.Duration = new Duration(TimeSpan.FromMilliseconds(500)); animFadeOut.From = 1.0; animFadeOut.To = 0.0; Storyboard.SetTarget(animFadeOut, UITextBlock); Storyboard.SetTargetProperty(animFadeOut, new PropertyPath(TextBlock.OpacityProperty)); sbFadeOut.Children.Add(animFadeOut); sbFadeOut.Completed += (sndr, evtArgs) => { Console.WriteLine("Text Change 2: " + Text); UITextBlock.Text = Text; Storyboard sbFadeIn = new Storyboard(); DoubleAnimation animFadeIn = new DoubleAnimation(); animFadeIn.Duration = new Duration(TimeSpan.FromMilliseconds(500)); animFadeIn.From = 0.0; animFadeIn.To = 1.0; Storyboard.SetTarget(animFadeIn, UITextBlock); Storyboard.SetTargetProperty(animFadeIn, new PropertyPath(TextBlock.OpacityProperty)); sbFadeIn.Children.Add(animFadeIn); sbFadeIn.Begin(); }; sbFadeOut.Begin(); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); throw; } } 

Here is the data that was output. Notice that "Films" does not get fired.

Text Change 1: One moment please…

Text Change 1: Films

Text Change 1: 1

Text Change 1: of

Text Change 1: 27

Text Change 2: One moment please…

Text Change 2: 1

Text Change 2: of

Text Change 2: 27

by JamesWjRose via /r/csharp

Leave a Reply