Пример #1
0
 protected virtual void Run()
 {
     try
     {
         RunPrepare();
         while (!_toExit)
         {
             try
             {
                 _cancellationTokenSource.Token.ThrowIfCancellationRequested();
                 if (_suspend)
                 {
                     _semaphore.WaitOne();
                     _suspend = false;
                 }
                 _cancellationTokenSource.Token.ThrowIfCancellationRequested();
                 if (_workDelegate != null)
                 {
                     _workDelegate.Invoke();
                 }
                 else
                 {
                     Work();
                 }
                 _cancellationTokenSource.Token.ThrowIfCancellationRequested();
                 if (WorkInterval > 0)
                 {
                     Sleep(WorkInterval);
                 }
             }
             catch (OperationCanceledException)
             {
                 break;
             }
             catch (ThreadInterruptedException)
             {
                 break;
             }
             catch (System.Exception ex)
             {
                 Console.Write(ex.Message);
             }
         }
         RunCompleted();
     }
     catch (Exception ex)
     {
         _logger.Error(ex);
         //System.IO.File.AppendAllText(System.AppDomain.CurrentDomain.BaseDirectory + "/log/taskError.txt",
         //    ex.InnerException.Message + "\r\n" + ex.InnerException.StackTrace);
     }
 }
Пример #2
0
 protected virtual void Run()
 {
     try
     {
         RunPrepare();
         while (!ToExit)
         {
             try
             {
                 CancellationTokenSource.Token.ThrowIfCancellationRequested();
                 if (Suspended)
                 {
                     Semaphore.WaitOne();
                     Suspended = false;
                 }
                 CancellationTokenSource.Token.ThrowIfCancellationRequested();
                 if (_workDelegate != null)
                 {
                     _workDelegate.Invoke();
                 }
                 else
                 {
                     Work();
                 }
                 CancellationTokenSource.Token.ThrowIfCancellationRequested();
                 if (WorkInterval > 0)
                 {
                     Sleep(WorkInterval);
                 }
             }
             catch (OperationCanceledException)
             {
                 break;
             }
             catch (ThreadInterruptedException)
             {
                 break;
             }
             catch (Exception ex)
             {
                 Console.Write(ex.Message);
             }
         }
         RunCompleted();
     }
     catch (Exception ex)
     {
         _logger.LogError(ex, $"TaskWork run failed!");
     }
 }
Пример #3
0
 protected virtual void Run()
 {
     while (!_toExit)
     {
         try
         {
             _cancellationTokenSource.Token.ThrowIfCancellationRequested();
             if (_suspend)
             {
                 _semaphore.WaitOne();
                 _suspend = false;
             }
             _cancellationTokenSource.Token.ThrowIfCancellationRequested();
             if (_workDelegate != null)
             {
                 _workDelegate.Invoke();
             }
             else
             {
                 Work();
             }
             _cancellationTokenSource.Token.ThrowIfCancellationRequested();
             if (WorkInterval > 0)
             {
                 Sleep(WorkInterval);
             }
         }
         catch (OperationCanceledException)
         {
             break;
         }
         catch (ThreadInterruptedException)
         {
             break;
         }
         catch (System.Exception ex)
         {
             Console.Write(ex.Message);
         }
     }
 }