How to Create a Timer in Wpf

Where is the WPF Timer control?

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

How do I create a timer in WPF?

In WPF, you use a DispatcherTimer.

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

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

How do I create a timer for WPF

You should use a DispatcherTimer:

using System.Windows.Threading;
...

DispatcherTimer timer = new DispatcherTimer();
timer.Tick += TimerTick;
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
...

private void TimerTick(object sender, EventArgs e)
{
// Put some code here
}

How to make a wpf countdown timer?

You can use DispatcherTimer class (msdn).

Duration of time you can hold in TimeSpan structure (msdn).

If you want formatting TimeSpan to hh:mm:ss you should invoke ToString method with "c" argument (msdn).

Example:

XAML:

<Window x:Class="CountdownTimer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Name="tbTime" />
</Grid>
</Window>

Code-behind:

using System;
using System.Windows;
using System.Windows.Threading;

namespace CountdownTimer
{
public partial class MainWindow : Window
{
DispatcherTimer _timer;
TimeSpan _time;

public MainWindow()
{
InitializeComponent();

_time = TimeSpan.FromSeconds(10);

_timer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
{
tbTime.Text = _time.ToString("c");
if (_time == TimeSpan.Zero) _timer.Stop();
_time = _time.Add(TimeSpan.FromSeconds(-1));
}, Application.Current.Dispatcher);

_timer.Start();
}
}
}

Creating stopwatch in WPF - time precison

This example can be quite useful for you.

public partial class MainWindow: Window   
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
Stopwatch stopWatch= new Stopwatch();
string currentTime = string.Empty;
public MainWindow()
{
InitializeComponent();
dispatcherTimer .Tick += new EventHandler(dt_Tick);
dispatcherTimer .Interval = new TimeSpan(0, 0, 0, 0, 1);
}

void dt_Tick(object sender, EventArgs e)
{
if (stopWatch.IsRunning)
{
TimeSpan ts = stopWatch.Elapsed;
currentTime = String.Format("{0:00}:{1:00}:{2:00}",
ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
clocktxt.Text = currentTime;
}
}

private void startbtn_Click(object sender, RoutedEventArgs e)
{
stopWatch.Start();
dispatcherTimer .Start();
}

private void stopbtn_Click(object sender, RoutedEventArgs e)
{
if (stopWatch.IsRunning)
{
stopWatch.Stop();
}
elapsedtimeitem.Items.Add(currentTime);
}

private void resetbtn_Click(object sender, RoutedEventArgs e)
{
stopWatch.Reset();
clocktxt.Text = "00:00:00";
}
}

How do I create a Timer in C#?

Use a DispatcherTimer:

using System.Windows.Threading;
...

var t = new DispatcherTimer();

t.Interval = TimeSpan.FromSeconds(15);
t.Tick += timer_Tick;
t.Start();

void timer_Tick(object sender, EventArgs e)
{
}

WPF / C# (No MVVM) Timer with Countdown Clock

You don't actually need more than a simple DispatcherTimer and a DateTime that is cyclically reset to the current time + 300 seconds.

public partial class MainWindow : Window
{
private readonly DispatcherTimer timer = new DispatcherTimer();
private DateTime endTime;

public MainWindow()
{
InitializeComponent();

timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += new EventHandler(OnTimerTick);
timer.Start();
}

private void OnTimerTick(object sender, EventArgs e)
{
var now = DateTime.Now;

if (endTime < now)
{
endTime = now.AddSeconds(300);
}

label.Content = (endTime - now).ToString(@"mm\:ss");
}
}

C# DispatchTimer WPF Countdown Timer

A better approach would be to do it with a TimeSpan rather than an int with a number. Setting the TimeSpan value in the following application will do the countdown as I want.

TimeSpan.FromMinutes for minutes

TimSpan.FromSeconds for seconds

You can check here for more detailed information.

public partial class MainWindow : Window
{
DispatcherTimer dispatcherTimer;
TimeSpan time;
public MainWindow()
{
InitializeComponent();

time = TimeSpan.FromMinutes(15);
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
dispatcherTimer.Tick += DispatcherTimer_Tick;
dispatcherTimer.Start();
}

private void DispatcherTimer_Tick(object sender, EventArgs e)
{
if (time == TimeSpan.Zero) dispatcherTimer.Stop();
else
{
time = time.Add(TimeSpan.FromSeconds(-1));
MyTime.Text = time.ToString("c");
}
}
}

Xaml Code

   <Grid>
<TextBlock Name="MyTime" />
</Grid>

WPF C# - Timer countdown

I've figured the complete code out as such:

DispatcherTimer _timer;

public MainWindow()
{
_myTimer = new DispatcherTimer();
_myTimer.Tick += MyTimerTick;
_myTimer.Interval = new TimeSpan(0,0,0,1);
}

private void ElementFlowSelectionChanged(object sender, SelectionChangedEventArgs e)
{
_counter = 0;
_myTimer.Stop();
_myTimer.Interval = new TimeSpan(0, 0, 0, 1);
_myTimer.Start();
}

private int _counter;
public int Counter
{
get { return _counter; }
set
{
_counter = value;
OnPropertyChanged("Counter");
}
}
private void MyTimerTick(object sender, EventArgs e)
{
Counter++;
if (Counter == 2)
{
_myTimer.Stop();
MessageBox.Show(“Reached the 2 second countdown”);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler e = PropertyChanged;
if (e != null)
{
e(this, new PropertyChangedEventArgs(propertyName));
}
}


Related Topics



Leave a reply



Submit