示例#1
0
 private void ExecutableTimer_Elapsed(object sender, ElapsedEventArgs e)
 {
     //Checking whether previous executing tasks have finished. If yes, return, else beginning new execution
     if (Interlocked.Exchange(ref _isLock, 1) != 0)
     {
         return;
     }
     Logger.WriteLog(LogLevel.Info, "Searching instances and collecting data have started");
     lock (_lockObject)
     {
         //Running tasks
         List <Task> serviceTasks = new List <Task>()
         {
             Task.Factory.StartNew(() => Searcher.InstanceSearch()),
             Task.Factory.StartNew(() => Collector.CollectData())
         };
         //Waiting for both tasks to finish and handling exceptions if have been raised
         try
         {
             Task.WaitAll(serviceTasks.ToArray());
             Logger.WriteLog(LogLevel.Info, "Searching instances and collecting data have successfully finished");
         }
         catch (AggregateException ex)
         {
             foreach (Exception innerException in ex.Flatten().InnerExceptions)
             {
                 Logger.WriteLog(LogLevel.Error, "Exception has occurred", innerException);
             }
         }
     }
     //Signaling that service can start new execution
     Interlocked.Exchange(ref _isLock, 0);
 }