title: Running a long task IHosted service at the same time and can manually turned on and off
body: <p>hi i am having a problem in running a long task for my background service using the IHostedService at first it really work fine but in the long run the background service suddenly stopped with this thread exited code:</p>
  
  <pre><code>The thread 10824 has exited with code 0 (0x0).
  The thread 12340 has exited with code 0 (0x0).
  The thread 9324 has exited with code 0 (0x0).
  The thread 11168 has exited with code 0 (0x0).
  The thread 11616 has exited with code 0 (0x0).
  The thread 9792 has exited with code 0 (0x0).
  </code></pre>
  
  <p>i register my background service as </p>
  
  <pre><code>//Register Background 
  serviceCollection.AddSingleton&lt;CoinPairBackgroundService&gt;
  serviceCollection.AddSingleton&lt;SaveFakePersonBackgroundService&gt;();
  serviceCollection.AddSingleton&lt;LeaderboardMinutesBackgroundService&gt;();
  serviceCollection.AddSingleton&lt;LeaderboardHoursBackgroundService&gt;();
  
  serviceCollection.AddSingleton&lt;IHostedService, CoinPairBackgroundService&gt;();
  serviceCollection.AddSingleton&lt;IHostedService, SaveFakePersonBackgroundService&gt;();
  serviceCollection.AddSingleton&lt;IHostedService, LeaderboardMinutesBackgroundService&gt;();
  serviceCollection.AddSingleton&lt;IHostedService, LeaderboardHoursBackgroundService&gt;();
  </code></pre>
  
  <p>because in the future i want to manually turned on and off my background services with use of </p>
  
  <pre><code>IServiceProvider provider = _serviceProvider.GetService&lt;MyBackgroundServiceHere&gt;();
  provider.StartAsync();
  provider.StopAsync
  </code></pre>
  
  <p>and this was my code in my background services StartAsync</p>
  
  <pre><code>    public Task StartAsync(CancellationToken cancellationToken)
      {
          _logger.LogDebug("Leaderboard minute ranking updates is starting");
          Task.Run(async () =&gt;
          {
              while (!cancellationToken.IsCancellationRequested)
              {
                  _logger.LogDebug("Leaderboard minute ranking updates  is dequeueing");
                  await DequeueRandomCustomers();
  
                  _logger.LogDebug("Leaderboard minute ranking updates  is enqueueing");
                  await EnqueueRandomCustomers();
  
                  _logger.LogDebug("Leaderboard minute ranking updates  thread is now sleeping");
                  //sleep for 1 minute
                  await Task.Delay(new TimeSpan(0, 0, 10));
              }
          });
          return Task.CompletedTask;
      }
  </code></pre>
  
  <p>I am confuse if there are some problems with registering my background service because i see my background service starts but in the long run it suddenly stopped i already get rid of those Thread.Sleep() in my background services hope you can help thanks in advance.</p>