190

Where can I find a control which is like the C# Timer Control in WPF?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
cetin
  • 2,025
  • 4
  • 17
  • 9

4 Answers4

350

The usual WPF timer is the DispatcherTimer, which is not a control but used in code. It basically works the same way like the WinForms timer:

System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0,0,1);
dispatcherTimer.Start();


private void dispatcherTimer_Tick(object sender, EventArgs e)
{
  // code goes here
}

More on the DispatcherTimer can be found here

marbel82
  • 925
  • 1
  • 18
  • 39
Gimno
  • 6,506
  • 3
  • 39
  • 39
  • Hi, i've been trying to use dispatch timer but i cant find it in my intelisense is there any reference that needs to be added for me to use it? – yohannist Oct 15 '12 at 22:26
  • 2
    I like the way you set the interval, better than milliseconds in my opinion. – JL. Aug 12 '13 at 02:48
  • Be sure to call dispatcherTimer.Stop() when you close your form. The WinForms version of the timer does that automatically. (That's the advantage of making the timer a Control.) If you don't you'll have a memory leak and possibly other bugs. – Trade-Ideas Philip Nov 28 '16 at 14:11
  • 8
    @JL Eh? That code is impossible to interpret. Far better is `var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };` – Jim Balter Jun 20 '17 at 05:11
  • In .NET 7.0 with VS 2022 I had to declare the timer function using nullable reference types like so. `private void dispatcherTimer_Tick(object? sender, EventArgs? e)` – Chris Arbogast Jan 27 '23 at 18:49
12

With Dispatcher you will need to include

using System.Windows.Threading;

Also note that if you right-click DispatcherTimer and click Resolve it should add the appropriate references.

Timwi
  • 65,159
  • 33
  • 165
  • 230
Malcor
  • 2,667
  • 21
  • 29
3

you can also use

using System.Timers;
using System.Threading;
0

The timer has special functions.

  1. Call an asynchronous timer or synchronous timer.
  2. Change the time interval
  3. Ability to cancel and resume  

if you use StartAsync () or Start (), the thread does not block the user interface element

     namespace UITimer


     {
        using thread = System.Threading;
        public class Timer
        {

        public event Action<thread::SynchronizationContext> TaskAsyncTick;
        public event Action Tick;
        public event Action AsyncTick;
        public int Interval { get; set; } = 1;
        private bool canceled = false;
        private bool canceling = false;
        public async void Start()
        {
            while(true)
            {

                if (!canceled)
                {
                    if (!canceling)
                    {
                        await Task.Delay(Interval);
                        Tick.Invoke();
                    }
                }
                else
                {
                    canceled = false;
                    break;
                }
            }


        }
        public void Resume()
        {
            canceling = false;
        }
        public void Cancel()
        {
            canceling = true;
        }
        public async void StartAsyncTask(thread::SynchronizationContext 
        context)
        {

                while (true)
                {
                    if (!canceled)
                    {
                    if (!canceling)
                    {
                        await Task.Delay(Interval).ConfigureAwait(false);

                        TaskAsyncTick.Invoke(context);
                    }
                    }
                    else
                    {
                        canceled = false;
                        break;
                    }
                }

        }
        public void StartAsync()
        {
            thread::ThreadPool.QueueUserWorkItem((x) =>
            {
                while (true)
                {

                    if (!canceled)
                    {
                        if (!canceling)
                        {
                            thread::Thread.Sleep(Interval);

                    Application.Current.Dispatcher.Invoke(AsyncTick);
                        }
                    }
                    else
                    {
                        canceled = false;
                        break;
                    }
                }
            });
        }

        public void StartAsync(thread::SynchronizationContext context)
        {
            thread::ThreadPool.QueueUserWorkItem((x) =>
            {
                while(true)
                 {

                    if (!canceled)
                    {
                        if (!canceling)
                        {
                            thread::Thread.Sleep(Interval);
                            context.Post((xfail) => { AsyncTick.Invoke(); }, null);
                        }
                    }
                    else
                    {
                        canceled = false;
                        break;
                    }
                }
            });
        }
        public void Abort()
        {
            canceled = true;
        }
    }


     }
  • 1
    Can you explain your code? If you just post some code, people won't learn from it and just copy & paste some code from the web. – Robert Dec 27 '19 at 17:09