title: How to run multiple task in background service in .net core with different timer duration
body: <p>I am creating a worker service which will be run as a windows service. I have a requirement where I would like to invoke two tasks which may have different timers.</p>
  <p>Say <code>DoWork</code> should be called every 5 minutes and <code>DoAnotherWork</code> should be called every 10 minutes or so. These two tasks can run in parallel and are not dependant on each other.</p>
  <p>I was able to create task <code>DoWork</code> which can run after every 5 minutes. I am a bit confused about how to implement another task that will have different timer duration?</p>
  <pre><code>public class Worker : BackgroundService
  {        
      private readonly IServiceScopeFactory _scopeFactory;        
      private IDataLoaderService _dataLoaderService;        
  
      public override Task StartAsync(CancellationToken cancellationToken)
      {
          using var scope = _scopeFactory.CreateScope();
          _dataLoaderService = scope.ServiceProvider.GetRequiredService&lt;IDataLoaderService&gt;();                        
          return base.StartAsync(cancellationToken);
      }
  
      protected override async Task ExecuteAsync(CancellationToken stoppingToken)
      {            
          while (!stoppingToken.IsCancellationRequested)
          {
              await DoWork(stoppingToken, _dataLoaderService);
              await Task.Delay(300000, stoppingToken); //Run every 5 minutes
  
              await DoAnotherWork(stoppingToken, _dataLoaderService);
              await Task.Delay(600000, stoppingToken); //Run every 10 minutes
          }
      }
  
      private async Task DoWork(CancellationToken stoppingToken, IDataLoaderService loaderService)
      {
          await loaderService.Process();            
      }        
      
      private async Task DoAnotherWork(CancellationToken stoppingToken, IDataLoaderService loaderService)
      {
          await loaderService.Validate();            
      }
  }
  </code></pre>