public static Task Delay(int millisecondsDelay, CancellationToken token = default(CancellationToken)) { if (millisecondsDelay < 0) { throw new ArgumentOutOfRangeException("millisecondsDelay", millisecondsDelay, "The value cannot be less than 0."); } if (millisecondsDelay == 0) { return(TaskDone); } token.ThrowIfCancellationRequested(); // allocate an object to hold the callback in the async state. object[] state = new object[1]; var completionSource = new TaskCompletionSource <object>(state); MultimediaTimerCallback callback = (uint id, uint msg, ref uint uCtx, uint rsv1, uint rsv2) => { // Note we don't need to kill the timer for one-off events. completionSource.TrySetResult(null); }; state[0] = callback; UInt32 userCtx = 0; var timerId = NativeMethods.TimeSetEvent((uint)millisecondsDelay, (uint)0, callback, ref userCtx, EventTypeSingle); if (timerId == 0) { int error = Marshal.GetLastWin32Error(); throw new Win32Exception(error); } return(completionSource.Task); }
///<example>await AVActions.TaskDelayMs(1);</example> ///<summary>High resolution delay, only use for sub 50ms.</summary> public static void TaskDelayMs(uint millisecondsDelay) { try { IntPtr createEvent = CreateEvent(IntPtr.Zero, true, false, null); MultimediaTimerCallback callbackDone = delegate { SetEvent(createEvent); CloseHandle(createEvent); }; timeSetEvent(millisecondsDelay, 0, callbackDone, 0, 0); WaitForSingleObject(createEvent, INFINITE); callbackDone.EndInvoke(null); } catch { } }
public MultimediaTimer() { Callback = new MultimediaTimerCallback(TimerCallbackMethod); Resolution = 5; Interval = 10; }
internal static extern UInt32 TimeSetEvent(UInt32 msDelay, UInt32 msResolution, MultimediaTimerCallback callback, ref UInt32 userCtx, UInt32 eventType);
public MultimediaTimer() { _callback = TimerCallbackMethod; Resolution = 5; Interval = 10; }
public WindowsTimer() { this.callback = new MultimediaTimerCallback(TimerCallbackMethod); Resolution = 5; Interval = 10; }
internal static extern uint timeSetEvent(uint msDelay, uint msResolution, MultimediaTimerCallback callback, uint userCtx, uint eventType);