Click here to Skip to main content
Click here to Skip to main content

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.

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)

Share

About the Author

Stanislav Kniazev

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionPerfect - Works well and clean code. Pinmemberpc180028-Sep-11 14:58 
AnswerRe: Perfect - Works well and clean code. PinmemberStanislav Kniazev29-Sep-11 0:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web01 | 2.8.150224.1 | Last Updated 17 Nov 2008
Article Copyright 2008 by Stanislav Kniazev
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid