internal Timer(TimerQueue queue, TimerMode mode, TimerExecutionContext context, bool isLongRunning, uint dueTime, uint period, TimerCallback callback) { Debug.Assert(queue != null); Debug.Assert(callback != null); Queue = queue; Mode = mode; ExecutionContext = context; IsLongRunning = isLongRunning; Period = period; DueTime = dueTime; Callback = callback; }
private Timer InternalCreateTimer( TimerMode mode, TimerExecutionContext context, TimerCallback callback, uint dueTime, uint period, bool isLongRunning ) { Debug.Assert( _hQueue != IntPtr.Zero ); if( _hQueue == IntPtr.Zero ) throw new InvalidOperationException( "The timer queue has already been disposed." ); Debug.Assert( callback != null ); if( callback == null ) throw new ArgumentNullException( "callback" ); switch( mode ) { case TimerMode.OneShot: Debug.Assert( dueTime > 0 ); Debug.Assert( period == 0 ); if( dueTime <= 0 ) throw new ArgumentOutOfRangeException( "dueTime", dueTime, "One-shot timers require a due time of 1ms or more." ); break; case TimerMode.Periodic: Debug.Assert( period > 0 ); if( period <= 0 ) throw new ArgumentOutOfRangeException( "period", period, "Periodic timers require a period of 1ms or more." ); break; } NativeMethods.TimerQueueFlags flags = ( NativeMethods.TimerQueueFlags )( ( uint )mode | ( uint )context ); if( isLongRunning == true ) flags |= NativeMethods.TimerQueueFlags.WT_EXECUTELONGFUNCTION; Timer timer = new Timer( this, mode, context, isLongRunning, dueTime, period, callback ); lock( _syncRoot ) { timer.Entry = _timers.Enqueue( timer ); timer.ID = _timerId++; } IntPtr handle = IntPtr.Zero; bool result = NativeMethods.CreateTimerQueueTimer( ref handle, _hQueue, _delegate, new IntPtr( timer.ID ), dueTime, period, flags ); Debug.Assert( result == true ); if( result == false ) { lock( _syncRoot ) _timers.Remove( timer.Entry ); int error = Marshal.GetLastWin32Error(); throw new Win32Exception( error, "Unable to create timer instance." ); } timer.Handle = handle; return timer; }
/// <summary> /// Create a new periodic timer that will fire at the given interval after the <paramref name="dueTime"/> has passed. /// </summary> /// <param name="callback">The method that will handle the timer execution.</param> /// <param name="dueTime">The time that will elapse before the timer is first executed, in milliseconds.</param> /// <param name="period">The time that will elapse between timer executions, in milliseconds.</param> /// <param name="executionContext">The thread that the callback will execute in.</param> /// <param name="isLongRunning"><c>true</c> if the callback execution is expected to take a long time.</param> /// <returns>A new <see cref="Timer"/> instance with the given parameters.</returns> public Timer CreatePeriodicTimer( TimerCallback callback, uint dueTime, uint period, TimerExecutionContext executionContext, bool isLongRunning ) { return this.InternalCreateTimer( TimerMode.Periodic, executionContext, callback, dueTime, period, isLongRunning ); }
/// <summary> /// Create a new one-shot timer that will fire at the given time. /// </summary> /// <param name="callback">The method that will handle the timer execution.</param> /// <param name="dueTime">The time that will elapse before the timer is executed, in milliseconds.</param> /// <param name="executionContext">The thread that the callback will execute in.</param> /// <param name="isLongRunning"><c>true</c> if the callback execution is expected to take a long time.</param> /// <returns>A new <see cref="Timer"/> instance with the given parameters.</returns> public Timer CreateOneShotTimer( TimerCallback callback, uint dueTime, TimerExecutionContext executionContext, bool isLongRunning ) { return this.InternalCreateTimer( TimerMode.OneShot, executionContext, callback, dueTime, 0, isLongRunning ); }
internal Timer( TimerQueue queue, TimerMode mode, TimerExecutionContext context, bool isLongRunning, uint dueTime, uint period, TimerCallback callback ) { Debug.Assert( queue != null ); Debug.Assert( callback != null ); Queue = queue; Mode = mode; ExecutionContext = context; IsLongRunning = isLongRunning; Period = period; DueTime = dueTime; Callback = callback; }
/// <summary> /// Create a new periodic timer that will fire at the given interval after the <paramref name="dueTime"/> has passed. /// </summary> /// <param name="callback">The method that will handle the timer execution.</param> /// <param name="dueTime">The time that will elapse before the timer is first executed, in milliseconds.</param> /// <param name="period">The time that will elapse between timer executions, in milliseconds.</param> /// <param name="executionContext">The thread that the callback will execute in.</param> /// <param name="isLongRunning"><c>true</c> if the callback execution is expected to take a long time.</param> /// <returns>A new <see cref="Timer"/> instance with the given parameters.</returns> public Timer CreatePeriodicTimer(TimerCallback callback, uint dueTime, uint period, TimerExecutionContext executionContext, bool isLongRunning) { return(this.InternalCreateTimer(TimerMode.Periodic, executionContext, callback, dueTime, period, isLongRunning)); }
/// <summary> /// Create a new one-shot timer that will fire at the given time. /// </summary> /// <param name="callback">The method that will handle the timer execution.</param> /// <param name="dueTime">The time that will elapse before the timer is executed, in milliseconds.</param> /// <param name="executionContext">The thread that the callback will execute in.</param> /// <param name="isLongRunning"><c>true</c> if the callback execution is expected to take a long time.</param> /// <returns>A new <see cref="Timer"/> instance with the given parameters.</returns> public Timer CreateOneShotTimer(TimerCallback callback, uint dueTime, TimerExecutionContext executionContext, bool isLongRunning) { return(this.InternalCreateTimer(TimerMode.OneShot, executionContext, callback, dueTime, 0, isLongRunning)); }
private Timer InternalCreateTimer(TimerMode mode, TimerExecutionContext context, TimerCallback callback, uint dueTime, uint period, bool isLongRunning) { Debug.Assert(_hQueue != IntPtr.Zero); if (_hQueue == IntPtr.Zero) { throw new InvalidOperationException("The timer queue has already been disposed."); } Debug.Assert(callback != null); if (callback == null) { throw new ArgumentNullException("callback"); } switch (mode) { case TimerMode.OneShot: Debug.Assert(dueTime > 0); Debug.Assert(period == 0); if (dueTime <= 0) { throw new ArgumentOutOfRangeException("dueTime", dueTime, "One-shot timers require a due time of 1ms or more."); } break; case TimerMode.Periodic: Debug.Assert(period > 0); if (period <= 0) { throw new ArgumentOutOfRangeException("period", period, "Periodic timers require a period of 1ms or more."); } break; } NativeMethods.TimerQueueFlags flags = (NativeMethods.TimerQueueFlags)(( uint )mode | ( uint )context); if (isLongRunning == true) { flags |= NativeMethods.TimerQueueFlags.WT_EXECUTELONGFUNCTION; } Timer timer = new Timer(this, mode, context, isLongRunning, dueTime, period, callback); lock ( _syncRoot ) { timer.Entry = _timers.Enqueue(timer); timer.ID = _timerId++; } IntPtr handle = IntPtr.Zero; bool result = NativeMethods.CreateTimerQueueTimer( ref handle, _hQueue, _delegate, new IntPtr(timer.ID), dueTime, period, flags); Debug.Assert(result == true); if (result == false) { lock (_syncRoot) _timers.Remove(timer.Entry); int error = Marshal.GetLastWin32Error(); throw new Win32Exception(error, "Unable to create timer instance."); } timer.Handle = handle; return(timer); }