Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 146259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:34:13+00:00 2026-05-11T08:34:13+00:00

With reference to this programming game I am currently building. I have a Class

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:34:14+00:00Added an answer on May 11, 2026 at 8:34 am

    Here’s one option, it will only work if the Robot.Run code is running in a different thread than the UI thread doing the animations.

    In the Robot.Ahead method (for example), use Dispatcher.Invoke (not BeginInvoke) to call the method that starts the animation, than add a lock on the robot with an empty block( lock(this) { } ).

    In the method that starts the animation call Monitor.Enter(robot) before starting the animation

    In the animation complete handler call Monitor.Leave(robot)

    The result will be

    time      robot thread          UI thread     |       ---------------       --------------     |       call Invoke    --->     |                             lock robot (Monitor.Enter)     |                             begin animation     |       Invoke returns <---   return     |       lock(this)            (animation running)     |       (wait for lock     |       to become available)     |     |                             Animation complete     |                             release lock (Monitor.Exit)     |       (lock available,     |       continue running)     |       Release lock (exit lock block)     |       Return and start next     |       movement     \/   
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 173k
  • Answers 173k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One of the ways this error occurs is if there… May 12, 2026 at 2:38 pm
  • Editorial Team
    Editorial Team added an answer If the order of the changes on the branch doesn't… May 12, 2026 at 2:38 pm
  • Editorial Team
    Editorial Team added an answer If you really want to redirect requests of /some_page.html to… May 12, 2026 at 2:38 pm

Related Questions

With reference to this programming game I am currently building. alt text http://img12.imageshack.us/img12/2089/shapetransformationf.jpg To
With reference to this programming game I am currently building. I am using WPF
With reference to this programming game I am currently building. I wrote the below
With reference to this programming game I am currently building. Thanks to the answers

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.