With reference to this programming game I am currently building.
I have a Class Library (dll) that will have a method Run
which will be composed of something like such:
public class MyRobot : Robot
{
public void Run(}
{
while (true)
{
Ahead(200); //moves the bot 200pixels
TurnLeft(90); //turns the bot by 90deg
}
}
}
In those methods (inherited from Robot
), the system will animate the robot using WPF (using BeginAnimation
or the DispatcherTimer
).
Now, the problem is that I don't a method to return (ie, move on to the next method) before completing the current one, because that will result in the animations taking place together, and when in an infinite loop (like the one above), that's especially not good.
My question is, what is the best way to prevent a method from returning before completing the animation ?
I currently have a bool
in the Robot
class (isActionRunning
) that be flagged to true
when an action starts running and then changes to false
in an animation callback (using the Completed
event if using BeginAnimation
).
At the end of each method (after invoking BeginAnimation
) I placed the following loop :
while (isActionRunning)
{
Thread.Sleep(200); //so that the thread sleeps for 200ms and then checks again if the animation is still running
}
This is so that the method won't return before the animation finishes.
But I feel that this is not the right way to do this.
Can anyone guide me to what's best to achieve this ?