title: WPF: Returning a method AFTER an animation has completed
body: <p><em>With reference to <a href="https://stackoverflow.com/questions/505040/developing-a-robocode-type-game-with-net-for-a-school-assignment">this programming game</a> I am currently building.</em></p>
  
  <p>I have a Class Library (dll) that will have a method <code>Run</code> which will be composed of something like such:</p>
  
  <pre><code>public class MyRobot : Robot 
  {
      public void Run(} 
      {
          while (true) 
          {
             Ahead(200); //moves the bot 200pixels
             TurnLeft(90); //turns the bot by 90deg 
          }
      }
  }
  </code></pre>
  
  <p>In those methods (inherited from <code>Robot</code>), the system will animate the robot using WPF (using <code>BeginAnimation</code> or the <code>DispatcherTimer</code>).</p>
  
  <p>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.</p>
  
  <hr>
  
  <p>My question is, <strong>what is the best way to prevent a method from returning before completing the animation</strong> ?</p>
  
  <p>I currently have a <code>bool</code> in the <code>Robot</code> class (<code>isActionRunning</code>) that be flagged to <code>true</code> when an action starts running and then changes to <code>false</code> in an animation callback (using the <code>Completed</code> event if using <code>BeginAnimation</code>).</p>
  
  <p>At the end of each method (after invoking <code>BeginAnimation</code>) I placed the following loop :</p>
  
  <pre><code>while (isActionRunning) 
  {
      Thread.Sleep(200); //so that the thread sleeps for 200ms and then checks again if the animation is still running 
  }
  </code></pre>
  
  <p>This is so that the method won't return before the animation finishes.</p>
  
  <p>But I feel that this is not the right way to do this.</p>
  
  <p>Can anyone guide me to what's best to achieve this ? </p>