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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:33:26+00:00 2026-06-03T10:33:26+00:00

I need a timer class which should provide functionality to start/stop/enable/disable and should be

  • 0

I need a timer class which should provide functionality to start/stop/enable/disable and should be NON UI.

I see 2 options

  1. DispatcherTimer -> has all functionality except that it is executed on UI thread

  2. ThreadPoolTimer -> No control over periodic timer.

I was thinking of creating wrapper over threadpool timer, but seems very complex as no/limited control over it.

How to design a timer class for the stated sceneario. (I require something similar to System.Threading.Timer).

  • 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-06-03T10:33:27+00:00Added an answer on June 3, 2026 at 10:33 am

    Since the UI should be very responsive in Metro apps – it might be enough to run a DispatcherTimer and call Task.Run() on things you want run on background thread.

    If that does not work for you – see if this does. It should have a similar API to the DispatcherTimer, but run on a different thread. Not that it keeps waiting for exactly the duration of the Interval set, so the timer will be notoriously late. It is also not exactly thread safe and I only played with it for 10 minutes, so it might not always work, but since it is open source – you can modify it to fit your bill.

    public class BackgroundTimer
    {
        private AutoResetEvent _stopRequestEvent;
        private AutoResetEvent _stoppedEvent;
    
        #region Interval
        private TimeSpan _interval;
        public TimeSpan Interval
        {
            get
            {
                return _interval;
            }
            set
            {
                if (IsEnabled)
                {
                    Stop();
                    _interval = value;
                    Start();
                }
                else
                {
                    _interval = value;
                }
            }
        }
        #endregion
    
        public event EventHandler<object> Tick;
    
        #region IsEnabled
        private bool _isEnabled;
        public bool IsEnabled
        {
            get
            {
                return _isEnabled;
            }
            set
            {
                if (_isEnabled == value)
                    return;
    
                if (value)
                    Start();
                else
                    Stop();
            }
        } 
        #endregion
    
        public BackgroundTimer()
        {
            _stopRequestEvent = new AutoResetEvent(false);
            _stoppedEvent = new AutoResetEvent(false);
        }
    
        public void Start()
        {
            if (_isEnabled)
            {
                return;
            }
    
            _isEnabled = true;
            _stopRequestEvent.Reset();
            Task.Run((Action)Run);
        }
    
        public void Stop()
        {
            if (!_isEnabled)
            {
                return;
            }
    
            _isEnabled = false;
            _stopRequestEvent.Set();
            _stoppedEvent.WaitOne();
        }
    
        private void Run()
        {
            while (_isEnabled)
            {
                _stopRequestEvent.WaitOne(_interval);
    
                if (_isEnabled &&
                    Tick != null)
                {
                    Tick(this, null);
                }
            }
    
            _stoppedEvent.Set();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a hard time with a class I am taking. We need to
I need to return two values at a time, so I have: class IterableObject(object):
I have the capability to extend a class at compile time, but I need
I need a timer tick with 1ms resolution under linux. It is used to
I need a Timer in a 'no form' Delphi unit (there's still a main
I'm writing an app that will need to make use of Timer s, but
I use a System.Timers.Timer in my Asp.Net application and I need to use the
I'd like to have a java.utils.Timer with a resettable time in java.I need to
sorry if this is pretty vague.im happy to provide any further information should it
My problem is: I have a class which accepts files in a directory. Whenever

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.