RecentComments

Comment RSS

Timers in WPF

by Ioannis 31. July 2009 07:44

There are times when you need a  task to execute periodically. There are two ways of achieving this:

  • Use the System.Windows.Threading.DispatcherTimer
  • Use the System.Threading.Timer

Say now that you want to implement a class the executes DoSomething() periodically and also informs through a delegate to whoever listens whent the execution is performed. The class for DispatcherTimer is as follows:

public class DTimer
{
    private DispatcherTimer timer;
    public event Action<int> DoSomething;
 
    private int _timesCalled = 0;
 
    public DTimer()
    {
        timer=new DispatcherTimer();
    }
    public void Start(int PeriodInSeconds)
    {
        timer.Interval = TimeSpan.FromSeconds(PeriodInSeconds);
        timer.Tick += timer_Task;
        _timesCalled = 0;
        timer.Start();
    }
 
    public void Stop()
    {
        timer.Stop();
    }
    private void timer_Task(object sender, EventArgs e)
    {
        _timesCalled++;
        DoSomething(_timesCalled);
 
    }
}

The same functionality for Timer is provided by the class below:

 

public class TTimer
{
 
    private Timer timer;
    public event Action<int> DoSomething;
    private int _timesCalled = 0;
 
    public TTimer()
    {}
    public void Start(int PeriodInSeconds)
    {
        timer = new System.Threading.Timer(timer_Task, null, 0,
        PeriodInSeconds*1000);
    }
    public void Stop()
    {
        timer.Dispose();
    }
    private void timer_Task(object State)
    {
        _timesCalled++;
        DoSomething(_timesCalled);
    }
}
Is there any difference? Well one important one is the fact that DoSomething() in the case of the DispatcherTimer is called in the same thread as the application while DoSomething() in the case of Timer is called in a new thread that was created for the timer. The two important implications of this are:
  • If DoSomething() manipulates GUI components then with the Timer you need to use: this.Dispatcher.Invoke((Action)delegate { //GUI RELATED CODE HERE} since you cannot access GUI controls from a different thread directly. With DispatcherTimer you do not need to do that.
  • If DoSomething() performas a time-consuming task, then the GUI will freeze in the case of the DispatcherTimer. In the case of the Timer it won't since the long methos is executed in a different thread

kick it on DotNetKicks.com

Shout it

Tags:

.NET | WPF

Comments

8/1/2009 6:10:08 AM #

trackback

Trackback from DotNetKicks.com

Timers in WPF

DotNetKicks.com

8/1/2009 6:12:31 AM #

trackback

Trackback from DotNetShoutout

C# and .NET Tips and Tricks | Timers in WPF

DotNetShoutout

8/3/2009 2:23:59 AM #

pingback

Pingback from rtipton.wordpress.com

Weekly Link Post 104 « Rhonda Tipton’s WebLog

rtipton.wordpress.com

Add comment


(Will show your Gravatar icon)

Enter the word
CAPTCHA word
Add 1 to the number above


  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7

Programming Blogs - BlogCatalog Blog Directory Add to Technorati Favorites

MVP Award

Ioannis Panagopoulos





This blog is using BlogEngine.Net and is hosted in the hoster below. I have not experienced any problems installing BlogEngine.Net in the host and I am satisfied with the host's response times. Therefore I recommend it.


DiscountASP Add