how to create WaitableTimer callback?
<p>I'm trying to create a periodical timer of eg 30min in C# that are able to wake the computer from sleep and to some work.</p>

<p>It seems like the Waitable timer function should be used after some research on the web.
I have made some test-code but it does not work as I was hoping for. It is possible to create the timer and the callback is called several times when I'm debugging and stepping through the code. But when i run the code without any breakpoints, the callback is only called a few times.</p>

<p>I have made a Form application that runs the "testWaitableTimer" when pushing a button. The "myTimerEvent" is supposed to print the time for each timer-event. 
I don't want to wait 30min timeout when testing so the period is set to 2sec interval and a due time of 5 seconds.</p>

<pre><code>    public void myTimerEvent(IntPtr complretionArg, UInt32 timerLow, UInt32 timerHigh)
    {
       SetText( DateTime.Now.ToString() + "hepp ");
    }

    public delegate void TimerCompleteDelegate(IntPtr complretionArg,
                                           UInt32 timerLow, UInt32 timerHigh);

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

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetWaitableTimer(SafeWaitHandle hTimer, [In] ref long pDueTime, int lPeriod, TimerCompleteDelegate pfnCompletionRoutine, IntPtr lpArgToCompletionRoutine, bool fResume);

    SafeWaitHandle handle;
    private void testWaitableTimer()
    {

        DateTime utc = DateTime.Now.AddSeconds(5);
        long duetime = utc.ToFileTime();

        using (handle = CreateWaitableTimer(IntPtr.Zero, true, "MyWaitabletimer"))
        {
            // create a periodic timer that will fire of the first event after 5sec and then every 2 sec
            if (SetWaitableTimer(handle, ref duetime, 2000, myTimerEvent, IntPtr.Zero, true))
            {
                using (EventWaitHandle wh = new EventWaitHandle(false, EventResetMode.AutoReset))
                {
                    wh.SafeWaitHandle = handle;
                    wh.WaitOne();                       // wait for first event
                    System.Threading.Thread.Sleep(2000);
                    wh.WaitOne();                       // wait for second event
                    System.Threading.Thread.Sleep(500);
                    wh.WaitOne();                       // wait for third event.

                }
            }
            else
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
    }
</code></pre>

<p>I'm pretty new to C# and have some questions:</p>

<ol>
<li><p>When running the code, the wh.WaitOne() is called several times, but it seems like it only waits for the first timer event. If all "sleep" functions are removed, the code will just wait for the first one and then jump over the rest.</p></li>
<li><p>I have created the safeWaitHandle outside of the testfunction. But it seems like the timer callback is only called when running the testfunction. and stops executing when exiting the test-function. Why is that?</p></li>
</ol>
