Click here to Skip to main content
5,787,682 members and growing! (20,000 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate License: The Code Project Open License (CPOL)

A WaitableTimer wrapper class

By Stanislav Kniazev

A C# wrapper for the Windows WaitableTimer.
C#, Windows, Win32, Visual Studio (VS2008, Visual Studio), Dev

Posted: 17 Nov 2008
Updated: 17 Nov 2008
Views: 3,361
Bookmarked: 24 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
8 votes for this Article.
Popularity: 3.69 Rating: 4.08 out of 5
1 vote, 12.5%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
2 votes, 25.0%
4
5 votes, 62.5%
5

Introduction

This class is a wrapper for the Windows waitable timer. It is very similar to System.Timers.Timer, with two important differences: it can wake up a computer from a suspended state (sleep or hibernate), and it supports larger interval values.

Background

I've been writing a class to replicate the Windows Task Scheduler functionality, so recurring events like "at 6PM on last Thursday of March, June, and September every ten minutes for an hour" could be managed programmatically. Upon some research, I found that the standard System.Timers.Timer (and the underlying System.Threading.Timer) is not very good to do the job as the interval is limited to 0xffffffff milliseconds (roughly 50 days), as illustrated by:

System.Timers.Timer tmr = new System.Timers.Timer();
tmr.Interval = double.MaxValue;
tmr.Start(); //System.ArgumentOutOfRangeException here at runtime

It is also not possible to resume a computer from power saving mode to execute the task, and this was critical enough for me to start looking at any available alternatives. The WaitableTimer wrapper class provides such an alternative for you. Enjoy!

Using the code

As you will see, the WaitableTimer class is very similar to System.Timers.Timer in regard to properties, methods, and events. There should be no learning curve as such, and the only new property introduced is ResumeSuspended. When true, this property tells the timer that it should wake up a computer to run the Elapsed event handlers:

using tevton.Win32Imports;
//....
WaitableTimer timer = new WaitableTimer();
timer.ResumeSuspended = true;
timer.Elapsed += 
    new WaitableTimer.ElapsedEventHandler(timer_Elapsed);
timer.Interval = 60000;
timer.Start();

As you can see, I had to recreate ElapsedEventHandler/ElapsedEventArgs as System.Timers.Timer's does not have a public constructor. The demo project attached will simulate the Sleep (and with one simple change, Hibernate) mode after starting the timer, so you will see it in action.

Notes

Due to the nature of Sleep/Hibernate modes, the timer will not be very accurate as the machine has to be completely awake to run the task, and this takes some time. You should also keep in mind that if you have a different OS first on your startup list (like the default Ubuntu on my machine), resuming from hibernation could unexpectedly load that OS.

The demo project is written in C# 2008, and will not compile properly in older versions, but the classes should not be dependent on the C# version.

References

The Sleep mode in the demo project is implemented via the WindowsController class by the KPD team. The XML comments are stolen from the System.Timers namespace. The WaitableTimer is documented by MSDN.

History

  • November 2008 - initial release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Stanislav Kniazev



Location: United Kingdom United Kingdom

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Nov 2008
Editor: Smitha Vijayan
Copyright 2008 by Stanislav Kniazev
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project