title: What happen after BackgroundService end his assigned task?
body: <p>Following the <a href="https://docs.microsoft.com/it-it/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.2" rel="nofollow noreferrer">docs</a> i implemented a library that allow to my web apis to create and run tasks in background. Basically, what i do is to create a <code>QueuedHostedService</code> (which inherit from <code>BackgroundService</code>) which contain a task to complete and add it to a queue. Then this queue get progressively emptied by running this tasks concurrently (on different threads i suppose). The following is the abstract class of the <code>BackgroundService</code></p>
  
  <pre><code>public abstract class BackgroundService : IHostedService, IDisposable
  {
      protected BackgroundService();
  
      public virtual void Dispose();
      //
      // Summary:
      //     Triggered when the application host is ready to start the service.
      //
      // Parameters:
      //   cancellationToken:
      //     Indicates that the start process has been aborted.
      public virtual Task StartAsync(CancellationToken cancellationToken);
      //
      // Summary:
      //     Triggered when the application host is performing a graceful shutdown.
      //
      // Parameters:
      //   cancellationToken:
      //     Indicates that the shutdown process should no longer be graceful.
      [AsyncStateMachine(typeof(&lt;StopAsync&gt;d__4))]
      public virtual Task StopAsync(CancellationToken cancellationToken);
      //
      // Summary:
      //     This method is called when the Microsoft.Extensions.Hosting.IHostedService starts.
      //     The implementation should return a task that represents the lifetime of the long
      //     running operation(s) being performed.
      //
      // Parameters:
      //   stoppingToken:
      //     Triggered when Microsoft.Extensions.Hosting.IHostedService.StopAsync(System.Threading.CancellationToken)
      //     is called.
      //
      // Returns:
      //     A System.Threading.Tasks.Task that represents the long running operations.
      protected abstract Task ExecuteAsync(CancellationToken stoppingToken);
  </code></pre>
  
  <p>The thing i don't know is, what happen exactly after the <code>Task</code> is completed? Do i need to do something (like using the Dispose method) or does the system take care of releasing the resource allocated to the background thread?</p>
  
  <p><strong>EDIT</strong> i'm adding the implementation of the class <code>QueuedHostedService</code> which implement the <code>ExecuteAsinc()</code> methods</p>
  
  <pre><code>public class QueuedHostedService : BackgroundService
  {
      private static ILog logger = LogExtensions.GetClassLogger();
  
      public QueuedHostedService(IBackgroundTaskQueue taskQueue)
      {
          TaskQueue = taskQueue;
      }
  
      public IBackgroundTaskQueue TaskQueue { get; }
  
      protected async override Task ExecuteAsync(CancellationToken cancellationToken)
      {
          logger.MethodEnter();
  
          while (!cancellationToken.IsCancellationRequested)
          {
              var workItem = await TaskQueue.DequeueAsync(cancellationToken);
  
              try
              {
                  await workItem(cancellationToken);
              }
              catch (Exception ex)
              {
                  logger.Error($"Error occurred executing {nameof(workItem)}. with the following exception {ex.Message}");
              }
          }
  
          logger.MethodLeave();
      }
  }
  </code></pre>