I have a For..Each loop which loops trough all grid's rectangles and changes the fill of them randomly. I change the fill with a color animation. Here's my code:
Dim rand as new random
Dim changeColor As New Animation.ColorAnimation
changeColor.Duration = TimeSpan.FromSeconds(0.5)
For Each r As Rectangle In Grid.Children
changeColor.From = ColorConverter.ConvertFromString(r.Fill.ToString)
Dim i As Integer = rand.Next(0, 2)
Select Case i
Case 0
changeColor.To = Colors.White
Case 1
changeColor.To = Colors.Gray
End Select
Animation.Storyboard.SetTarget(changeColor, r)
Animation.Storyboard.SetTargetProperty(changeColor, New PropertyPath("Fill.Color"))
Dim sb As New Animation.Storyboard
sb.Children.Add(changeColor)
sb.Begin()
System.Threading.Thread.Sleep(0.5)
Next
The problem is that the loop doesn't sleep. I want to trigger the animation, then wait until the rectangle fill is changed and then continue with the rest, but it seems that all of the rectangles fill are changed in the same time. So what I'm doing wrong?