Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A WaitableTimer wrapper class

0.00/5 (No votes)
17 Nov 2008 1  
A C# wrapper for the Windows WaitableTimer.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here