Click here to Skip to main content
12,701,314 members (34,381 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked

A WaitableTimer wrapper class

, 17 Nov 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A C# wrapper for the Windows WaitableTimer.


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.


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;

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.


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.


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.


  • November 2008 - initial release.


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


About the Author

Stanislav Kniazev
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionPerfect - Works well and clean code. Pin
pc180028-Sep-11 14:58
memberpc180028-Sep-11 14:58 
AnswerRe: Perfect - Works well and clean code. Pin
Stanislav Kniazev29-Sep-11 0:56
memberStanislav Kniazev29-Sep-11 0: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 17 Nov 2008
Article Copyright 2008 by Stanislav Kniazev
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid