예제 #1
0
 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;
         }
     }
 }
예제 #2
0
 protected void Cancel()
 {
     using (CurrentLogger.ScopedTrace($"{GetType()}.{MethodBase.GetCurrentMethod()}"))
     {
         _cancelSource.Cancel();
     }
 }
예제 #3
0
 public void StopNoThrow()
 {
     using (CurrentLogger.ScopedTrace($"{GetType()}.{MethodBase.GetCurrentMethod()}"))
     {
         lock (SyncRoot)
         {
             if (_disposed)
             {
                 return;
             }
             ExceptionHelper.ExceptionCatcher(InternalStop, where : nameof(LongRunningTask) + ":" + GetType() + "." + nameof(StopNoThrow));
         }
     }
 }
예제 #4
0
 public void Stop()
 {
     using (CurrentLogger.ScopedTrace($"{GetType()}.{MethodBase.GetCurrentMethod()}"))
     {
         lock (SyncRoot)
         {
             if (_disposed)
             {
                 throw new ObjectDisposedException(nameof(LongRunningTask));
             }
             InternalStop();
         }
     }
 }
예제 #5
0
 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();
         }
     }
 }
예제 #6
0
 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);
         }
     }
 }
예제 #7
0
 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));
         }
     }
 }