/// <summary> /// Initializes a new instance of the <see cref="TimedIntervalTask"/> class. /// </summary> /// <param name="interval">The interval between executions.</param> /// <param name="handler">The handler executed when the interval has elapsed.</param> /// <param name="target">The optional target object instance the task executes against.</param> public TimedIntervalTask(ITimedInterval interval, IntervalExecutionHandler handler, object target = null) { callback = handler; instance = target; Interval = interval; lastExecution = DateTime.Now; sync = new object(); }
/// <summary> /// Registers a method invocation as a timed task within the context. /// </summary> /// <param name="interval">The interval between method executions.</param> /// <param name="method">The method which must execute.</param> /// <param name="instance">The object instance which the method must execute against.</param> public void Register(ITimedInterval interval, MethodInfo method, object instance) { if (interval == null) { throw new ArgumentException("Cannot register a timed task with no interval", "interval"); } if (instance == null && !method.IsStatic) { throw new ArgumentException($"Cannot register a non-static method without a target instance for '{method.DeclaringType.FullName}.{method.Name}'"); } var handler = CreateTimedIntervalHandler(method); var task = new TimedIntervalTask(interval, handler, instance); timedTasks.Add(task); timedThread.Add(task); }