/// <summary> /// The event will be called after the specified amount of milliseconds /// </summary> public static void Register(ScheduledEventCallbackEventHandler e, int millisToNext) { if (contextId != Thread.CurrentThread.ManagedThreadId) { context.Send( delegate { Register(e, millisToNext); }, null); return; } Node n; if (!events.TryGetValue(e, out n)) { n = new Node(e); events[e] = n; count++; } else { if (first == n) { if (first == last) { first = last = null; } else { first = n.next; } } else if (last == n) { last = n.previous; } n.Detach(); } n.ticks = DateTime.UtcNow.Ticks / 10000 + millisToNext + 1; if (Insert(n)) { if (millisToNext >= int.MaxValue) { timer.Interval = int.MaxValue; } else { timer.Interval = millisToNext + 1; } timer.Enabled = true; } }
public static void Unregister(ScheduledEventCallbackEventHandler e) { if (contextId != Thread.CurrentThread.ManagedThreadId) { context.Send( delegate { Unregister(e); }, null); return; } Node n; if (!events.TryGetValue(e, out n)) { return; } events.Remove(e); if (count == 1) { first = last = null; } else { if (first == n) { first = n.next; } else if (last == n) { last = n.previous; } n.Detach(); } count--; }
public static DateTime GetDate(ScheduledEventCallbackEventHandler e) { if (contextId != Thread.CurrentThread.ManagedThreadId) { var d = DateTime.MaxValue; context.Send( delegate { d = GetDate(e); }, null); return(d); } Node n; if (!events.TryGetValue(e, out n)) { return(DateTime.MaxValue); } return(new DateTime((n.ticks - 1) * 10000, DateTimeKind.Utc)); }
public Node(ScheduledEventCallbackEventHandler callback) { this.callback = callback; }