/// <summary> /// Schedules a one-time event. /// </summary> /// <param name="Callback">Method to call when event is due.</param> /// <param name="When">When the event is to be executed.</param> /// <param name="State">State object</param> /// <returns>Timepoint of when event was scheduled.</returns> public static DateTime ScheduleEvent(ScheduledEventCallback Callback, DateTime When, object State) { if (scheduler != null) { return(scheduler.Add(When, Callback, State)); } else { return(DateTime.MinValue); } }
/// <summary> /// Adds an event. /// </summary> /// <param name="When">When to execute the event.</param> /// <param name="Callback">Method called when event is to be executed.</param> /// <param name="State">State object bassed to <paramref name="Callback"/>.</param> /// <returns>Time when event was scheduled. May differ from <paramref name="When"/> by a few ticks, to make sure the timestamp is unique.</returns> public DateTime Add(DateTime When, ScheduledEventCallback Callback, object State) { lock (this.events) { while (this.events.ContainsKey(When)) { When = When.AddTicks(this.gen.Next(1, 10)); } this.events[When] = new ScheduledEvent(When, Callback, State); this.RecalcTimerLocked(); } return(When); }
// where T : ScheduledEventCallbackInfo /// <summary>Schedule an event to occur. The callback will be invoked with the provided parameters</summary> /// <returns>A unique identifier such that it can be cancelled later</returns> public Guid ScheduleEvent(DateTime scheduleTime, ScheduledEventCallback callback, ScheduledEventCallbackInfo callbackInfo) { PerformDisposedCheck(); lock (lockObj) { List<object> list = null; if (this.scheduleTable.ContainsKey(scheduleTime)) list = this.scheduleTable[scheduleTime]; else { list = new List<object>(); this.scheduleTable[scheduleTime] = list; } Guid guid = Guid.NewGuid(); list.Add(new object[] {callback, callbackInfo, guid}); if (scheduleTime < nextScheduledItem) { // wake up the scheduler to update the schedule and possibly notify Monitor.PulseAll(lockObj); } return guid; } }
/// <summary> /// Contains information about a scheduled event. /// </summary> /// <param name="When">When an event is to be executed.</param> /// <param name="EventMethod">Method to call when event is executed.</param> /// <param name="State">State object passed on to <paramref name="EventMethod"/>.</param> public ScheduledEvent(DateTime When, ScheduledEventCallback EventMethod, object State) { this.when = When; this.eventMethod = EventMethod; this.state = State; }
/// <summary> /// Contains information about a scheduled event. /// </summary> /// <param name="When">When an event is to be executed.</param> /// <param name="EventMethod">Method to call when event is executed.</param> /// <param name="State">State object passed on to <paramref name="EventMethod"/>.</param> public ScheduledEvent(DateTime When, ScheduledEventCallbackAsync EventMethod, object State) { this.when = When; this.eventMethod = this.AsyncCallback; this.state = new object[] { EventMethod, State }; }