1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
private IntPtr handle;
private void shutdownToolStripMenuItem_Click(object sender, EventArgs e) { DateTime dt = DateTime.Now; dt = dt.AddSeconds(30.0); long duetime = dt.ToFileTime();
handle = Win32.PowerManagement.CreateWaitableTimer(IntPtr.Zero, true, ""); Win32.PowerManagement.SetWaitableTimer(handle, ref duetime, 0, null, IntPtr.Zero, true);
Thread t = new Thread(new ThreadStart(this.NewThread)); t.Start(); }
private void NewThread() { int ret = Win32.PowerManagement.WaitForSingleObject(handle, (int)Win32.PowerManagement.INFINITE); MessageBox.Show("Wait object"); }
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) { TimerCompleteDelegate TimerComplete = new TimerCompleteDelegate(TimerCompleted); TimerSetDelegate TimerSet = new TimerSetDelegate(TimerIsSet);
Console.WriteLine("Creating WaitableTimer"); handle = CreateWaitableTimer(IntPtr.Zero, true, "WaitableTimer"); Console.WriteLine("Last Error = " + Marshal.GetLastWin32Error().ToString());
Console.WriteLine("Setting WaitableTimer"); SetWaitableTimer(handle, ref Interval, 0, TimerComplete, IntPtr.Zero, true); Console.WriteLine("Last Error = " + Marshal.GetLastWin32Error().ToString()); Console.WriteLine("Timer set @ " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
Thread t_Wait = new Thread(new ThreadStart(WaitTimer)); t_Wait.Start();
if (OnTimerSet != null) { OnTimerSet(); } }
private void WaitTimer() { 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")); }
CloseHandle(handle);
if (OnTimerCompleted != null) { OnTimerCompleted(); } }
private void TimerCompleted() { Console.WriteLine("Timer is complete in the class"); }
private void TimerIsSet() { Console.WriteLine("Timer is set in the class"); } }
|