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 7437021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:19:57+00:00 2026-05-29T10:19:57+00:00

I never worker too much with timers and delays, so I’m pretty sure I’m

  • 0

I never worker too much with timers and delays, so I’m pretty sure I’m doing something really really ugly with my code but I can’t figure out a solution.

Ok… I have a class called AnimatorsPool, which contains and handles a lot of Animator classes at once. Animator is just a simple, timer based, control mover.

    public sealed class AnimatorsPool : IDisposable
    {
        private Boolean m_Disposed;
        private Boolean m_Stopped;
        private Boolean m_Waiting;
        private Int32 m_DelayBetween;
        private Int32 m_DelayStart;
        private List<Animator> m_Animators;
        private Stopwatch m_Stopwatch;

        public Boolean AnimationsFinished
        {
            get
            {
                foreach (Animator animator in m_Animators)
                {
                    if (!animator.AnimationFinished)
                        return false;
                }

                return true;
            }
        }

        public AnimatorsPool(Int32 delayBetween, Int32 delayStart)
        {
            m_Disposed = false;
            m_Stopped = false;
            m_Waiting = false;
            m_DelayBetween = ((delayBetween < 0) ? 0 : delayBetween);
            m_DelayStart = ((delayStart < 0) ? 0 : delayStart);
            m_Animators = new List<Animator>();
        }

        private void Wait(Int32 delay)
        {
            m_Stopwatch = Stopwatch.StartNew();

            m_Waiting = true;

            while (!m_Stopped && (m_Stopwatch.ElapsedMilliseconds <= delay))
                Application.DoEvents();

            m_Waiting = false;

            m_Stopwatch.Stop();
        }

        public void Add(Animator animator)
        {
            m_Animators.Add(animator);
        }

        public void Dispose()
        {
            if (!m_Disposed)
            {
                foreach (Animator animator in m_Animators)
                    animator.Dispose();

                m_Animators.Clear();
                m_Animators = null;

                m_Disposed = true;
            }

            GC.SuppressFinalize(this);
        }

        public void Start()
        {
            if (m_Animators.Count > 0)
            {
                if (m_DelayStart > 0)
                    Wait(m_DelayStart);

                m_Animators[0].Start();

                if (m_Animators.Count > 1)
                {
                    for (Int32 i = 1, length = m_Animators.Count; i < length; ++i)
                    {
                        if (m_DelayBetween > 0)
                            Wait(m_DelayBetween);

                        m_Animators[i].Start();
                    }
                }
            }
        }

        public void Stop()
        {
            m_Stopped = true;

            while (m_Waiting)
                Application.DoEvents();
        }
    }

Inside my main form, which is sometimes using AnimatorsPool, I use the following code:

        using (AnimatorsPool pool = new AnimatorsPool(100, (delayAction ? 100 : 0)))
        {
            m_AnimatorsPools.Add(pool);

            for (Int32 i = (controlsCount - 1); i >= 0; --i)
            {
                Animator animator = new Animator(controls[i], coordinates[i], 50);
                pool.Add(animator);
            }

            pool.Start();

            while (!pool.AnimationsFinished)
                Application.DoEvents();

            m_AnimatorsPools.Remove(pool);
        }

Ok now… what happened in the beginning was that, if I exited the program while AnimatorsPool was still running Wait() method, my main form was disappearing but the process was still running undefinately in my task manager because of that.
So I implemented a Dispose override in my main form:

    protected override void Dispose(Boolean disposing)
    {
        if (!m_Disposed)
        {
            if (disposing)
            {
                s_RandomProvider.Dispose();

                foreach (AnimatorsPool pool in m_AnimatorsPools)
                    pool.Dispose();

                m_AnimatorsPools.Clear();
                m_AnimatorsPools = null;
            }

            m_Disposed = true;
        }

        base.Dispose(disposing);
    }

Ok… now my program exits properly sometimes, but sometimes not. And what I can see from debug is that, when it happens, it’s because some AnimatorsPool is still running the Wait() method even if m_Stopped is set to true.
I think I should use threads, timers or something like that, but I’ve no experience. Any help is welcome! Thanks!

  • 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. Editorial Team
    Editorial Team
    2026-05-29T10:19:59+00:00Added an answer on May 29, 2026 at 10:19 am

    Whatever you do, don’t do this:

    while (!m_Stopped && (m_Stopwatch.ElapsedMilliseconds <= delay))
        Application.DoEvents();
    

    You should use timers and event-driven for this, instead of busy-waiting. That is only ever OK in embedded real-time stuff.

    You need to rewrite your code pretty much, and it is difficult to give you pointers without creating a lesson in timers and event-driven development.

    EDIT: From the Application.DoEvents documentation in MSDN (emphasis mine):

    Calling this method causes the current thread to be suspended while all waiting
    window messages are processed. If a message causes an event to be triggered, then
    other areas of your application code may execute. This can cause your application
    to exhibit unexpected behaviors that are difficult to debug.
    If you perform
    operations or computations that take a long time, it is often preferable to
    perform those operations on a new thread. For more information about asynchronous
    programming, see Asynchronous Programming Overview.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a really simple question but one I've never quite worked out
I never worked too much with sounds in Mma. I have t = Sound[List[Violin,SoundNote[-6]]]
I've never worked with percentage layouts. Of course, I already study them, but without
I'm trying to figure out an architecture for plugins, but I've never worked with
I successfully installed the latest QuantumGrid from DevExpress, but I've never worked with this
Usually I worked with PostgreSQL and never had a problem, but now I need
I am still fairly new to parallel computing so I am not too sure
I've used BackgroundWorkers quite a bit, but I've never experienced this problem before. My
So my site is starting to use too much resources. the core of my
this may come as pretty odd but I'm now working on a membership based

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.