Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / Win32
Article

A WaitableTimer wrapper class

Rate me:
Please Sign up or sign in to vote.
4.81/5 (10 votes)
17 Nov 2008CPOL2 min read 33.3K   486   32   2
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:

C#
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:

C#
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)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionPerfect - Works well and clean code. Pin
pc180028-Sep-11 13:58
pc180028-Sep-11 13:58 
AnswerRe: Perfect - Works well and clean code. Pin
Stanislav Kniazev28-Sep-11 23:56
Stanislav Kniazev28-Sep-11 23:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.