Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
setwaitabletimer (kernel32)
 
.
Summary

C# Signature:

[DllImport("kernel32.dll")]
static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long pDueTime,
   int lPeriod, TimerCompleteDelegate pfnCompletionRoutine,
   IntPtr lpArgToCompletionRoutine, bool fResume);

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

// Class that sets a WIN32 WaitableTimer. The timer will wake up the PC if the PC
// is in standby or hibernation mode.
class WaitableTimer
{
    public delegate void TimerSetDelegate();
    public delegate void TimerCompleteDelegate();

    public event TimerSetDelegate OnTimerSet;
    public event TimerCompleteDelegate OnTimerCompleted;

    [DllImport("kernel32.dll")]
    static extern IntPtr CreateWaitableTimer(IntPtr lpTimerAttributes, bool bManualReset, string lpTimerName);

    [DllImport("kernel32.dll")]
    static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long ft, int lPeriod, TimerCompleteDelegate pfnCompletionRoutine, IntPtr  pArgToCompletionRoutine, bool fResume);

    [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
    static extern Int32 WaitForSingleObject(IntPtr Handle, uint Wait);

    [DllImport("kernel32.dll")]
    static extern bool CancelWaitableTimer(IntPtr hTimer);

    [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool CloseHandle(IntPtr hObject);

    private IntPtr handle;
    private uint INFINITE = 0xFFFFFFFF;

    public void SetTimer(long Interval)
    {
        // Creating the timer delegate
        //
        TimerCompleteDelegate TimerComplete = new TimerCompleteDelegate(TimerCompleted);
        TimerSetDelegate TimerSet = new TimerSetDelegate(TimerIsSet);

        // Creating the timer
        //
        Console.WriteLine("Creating WaitableTimer");
        handle = CreateWaitableTimer(IntPtr.Zero, true, "WaitableTimer");
        Console.WriteLine("Last Error = " + Marshal.GetLastWin32Error().ToString());

        // Setting up the timer, the long Interval value needs to be negative if
        // you want to set up a delay in milliseconds. ie
        // if Interval = -60000000 the timer will expire in 1 minute. Once expired it runs the
        // TimerComplete delegate
        //
        Console.WriteLine("Setting WaitableTimer");
        SetWaitableTimer(handle, ref Interval, 0, TimerComplete, IntPtr.Zero, true);
        Console.WriteLine("Last Error = " + Marshal.GetLastWin32Error().ToString()); // The error may be 1004 (Invalid flags), this is not critical
        Console.WriteLine("Timer set @ " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));

        // Starting a new thread which waits for the WaitableTimer to expire
        //
        Thread t_Wait = new Thread(new ThreadStart(WaitTimer));
        t_Wait.Start();

        // Raising Event Timer Set
        //
        if (OnTimerSet != null)
        {
            OnTimerSet();
        }
    }

    private void WaitTimer()
    {
        // Waiting for the timer to expire
        //
        if (WaitForSingleObject(handle, INFINITE) != 0)
        {
            Console.WriteLine("Last Error = " + Marshal.GetLastWin32Error().ToString());
        }
        else
        {
            Console.WriteLine("Timer expired @ " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
        }

        // Closing the timer
        //
        CloseHandle(handle);

        // Raising event Timer Completed
        //
        if (OnTimerCompleted != null)
        {
            OnTimerCompleted();
        }
    }

    private void TimerCompleted()
    {
        // Routine executed once the timer has expired. This is executed independently of the
        // program calling this class implementation of the OnTimerCompleted Event
        //
        Console.WriteLine("Timer is complete in the class");
    }

    private void TimerIsSet()
    {
        Console.WriteLine("Timer is set in the class");
    }
}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions