/// <summary> /// Executes the specified working item on the scheduling engine. /// </summary> /// <param name="engine">The scheduling engine.</param> /// <param name="item">The scheduled item.</param> public void Execute(ISchedulerEngine engine, IWorkingItem item) { int workerThreads; int iocpThreads; ThreadPool.GetAvailableThreads(out workerThreads, out iocpThreads); if (workerThreads <= _warningThreshold) { _log.Warn("Number of free threads in threadpool is low. Next async tasks will be only queued!"); } // new task based threading Task.Factory.StartNew( () => { try { item.Execute(engine); } catch (Exception e) { // last resort exception handler _log.Error($"Unhandled exception in work item async processing {e.Message}"); } }, TaskCreationOptions.PreferFairness); }
/// <summary> /// Schedules the specified working item for execution. /// </summary> /// <param name="item">The working item.</param> public void Schedule(IWorkingItem item) { if (!_started) { var message = $"Scheduler is not started (trying to schedule '{item}')"; throw new InvalidOperationException(message); } DateTime plannedExecutionTime = item.ExecutionTime; // check that we're not trying to schedule a task that was already aborted if (plannedExecutionTime == DateTime.MinValue) { throw new ArgumentException($"Cannot schedule aborted task! ({item})"); } ////_log.Trace( //// "Scheduling the {0} to be fired at {1}", //// item.ToString(), //// plannedExecutionTime.ToString("dd.MM.yyyy HH:mm:ss.ffff")); // append the new item to the queue of pending working items _queueProcessor.Schedule(new[] { item }); // signal "something is in the queue" _delayedCallback.Signalize(); }
/// <summary> /// Starts the specified timer. /// </summary> /// <param name="timer">The timer.</param> /// <returns>The started working item.</returns> public static IWorkingItem Start(this IWorkingItem timer) { var scheduler = SchedulerRetrievalFunc(); if (scheduler == null) { throw new InvalidOperationException("No scheduler in context - todo create some (shared) here"); } scheduler.Schedule(timer); return(timer); }
/// <summary> /// Checks whether the timer is not null, and when it is not, aborts it. /// </summary> /// <param name="timer">The timer.</param> public static void Abort(this IWorkingItem timer) { timer?.Cancel(); }