public void Start() { using (CurrentLogger.ScopedTrace($"{GetType()}.{MethodBase.GetCurrentMethod()}")) { lock (SyncRoot) { if (_disposed) { throw new ObjectDisposedException(nameof(LongRunningTask)); } var task = _workerTask; if (task != null) { return; } _cancelSource?.Dispose(); _cancelSource = new CancellationTokenSource(); var cancelToken = _cancelSource.Token; CancelToken = cancelToken; Scheduling.TaskName = GetType().ToString(); Scheduling.ProcessBody = (token) => Process(token); task = new Task(() => Scheduling.SchedulingRun(cancelToken), cancelToken, TaskCreationOptions.LongRunning); task.Start(); _workerTask = task; } } }
protected void Cancel() { using (CurrentLogger.ScopedTrace($"{GetType()}.{MethodBase.GetCurrentMethod()}")) { _cancelSource.Cancel(); } }
public void StopNoThrow() { using (CurrentLogger.ScopedTrace($"{GetType()}.{MethodBase.GetCurrentMethod()}")) { lock (SyncRoot) { if (_disposed) { return; } ExceptionHelper.ExceptionCatcher(InternalStop, where : nameof(LongRunningTask) + ":" + GetType() + "." + nameof(StopNoThrow)); } } }
public void Stop() { using (CurrentLogger.ScopedTrace($"{GetType()}.{MethodBase.GetCurrentMethod()}")) { lock (SyncRoot) { if (_disposed) { throw new ObjectDisposedException(nameof(LongRunningTask)); } InternalStop(); } } }
public virtual void Dispose() { using (CurrentLogger.ScopedTrace($"{GetType()}.{MethodBase.GetCurrentMethod()}")) { lock (SyncRoot) { if (_disposed) { return; } _disposed = true; ExceptionHelper.ExceptionCatcher(InternalStop, where : nameof(LongRunningTask) + ":" + GetType() + "." + nameof(Dispose)); _cancelSource?.Dispose(); } } }
private void InternalStop() { using (CurrentLogger.ScopedTrace($"{GetType()}.{MethodBase.GetCurrentMethod()}")) { var task = _workerTask; if (task == null) { return; } _workerTask = null; _cancelSource.Cancel(); using (new RAII(task.Dispose)) { CurrentLogger.Trace($"Начинаем ожидание окончания задачи {GetType().Name}"); task.Wait(_stopTimeout); } } }
public void WaitForStopNoThrow(TimeSpan?stopTimeout = null) { using (CurrentLogger.ScopedTrace($"{GetType()}.{MethodBase.GetCurrentMethod()}")) { lock (SyncRoot) { if (_disposed) { return; } ExceptionHelper.ExceptionCatcher(() => { var task = _workerTask; if (task == null) { return; } CurrentLogger.Trace($"Начинаем ожидание окончания задачи {GetType().Name} без её завершения "); task.Wait(stopTimeout ?? TimeSpanConstants.Infinite); }, where : nameof(LongRunningTask) + ":" + GetType() + "." + nameof(StopNoThrow)); } } }