title: Background parallel tasks
body: <p>I have an console application .NET core which uses Background tasks with hosted services like described here : <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-6.0&amp;tabs=visual-studio#queued-background-tasks" rel="nofollow noreferrer">link</a></p>
  <p>I've used the code sample provided by Microsoft and added the IBackgroundTaskQueue, BackgroundTaskQueue and QueuedHostedService.</p>
  <p>In my Startup.cs, I'm registering  QueuedHostedService instance as follows:</p>
  <p><code>services.AddHostedService&lt;QueuedHostedService&gt;();</code></p>
  <p>And IBackgroundTaskQueue as follows :</p>
  <pre><code>services.AddSingleton&lt;IBackgroundTaskQueue&gt;(ctx =&gt; {
                          if (!int.TryParse(hostContext.Configuration[&quot;QueueCapacity&quot;], out var queueCapacity))
                              queueCapacity = 100;
                          return new BackgroundTaskQueue(queueCapacity);
  </code></pre>
  <p>I use MonitorLoop like Microsoft's article, which initialize my rabbitmq server to retrieve messages :</p>
  <pre><code> public void InitRabbit()
   {
      var uri = TdiRabbitServerConstants.GetServerUri(EnvironmentType.Development, ApplicationType.Translator);
      _rabbitMQService.ConnectToServerWithConsumersAsync(uri);
  
      _rabbitMQService.SetEventHandlerMessageAsync(Consumer_ReceivedAsync);
  
      _rabbitMQService.DeclareLinkOnChannel(Queues.QueueTranslatorEventNotificationResolver, null);
      _rabbitMQService.QueueSubscription(false);
  }
  </code></pre>
  <p>Tasks coming from RabbitMQ event which triggered as follows :</p>
  <pre><code>private async Task Consumer_ReceivedAsync(object sender, DataReceived @event)
   {
          Task.Run(async  () =&gt; await ProcessNewTask(@event));
   }
  </code></pre>
  <p>Tasks are enqueued and then dequeued and executed by the QueuedHostedService.
  QueuedHostedService implement this example and I use <strong>dbvega</strong> solution: <a href="https://stackoverflow.com/questions/62867167/asp-net-core-queued-background-tasks-parallel-processing">link</a></p>
  <pre><code>public Task StartAsync(CancellationToken cancellationToken)
          {
              _tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
  
              for (var i = 0; i &lt; _executorsCount; i++)
              {
                  var executorTask = new Task(
                      async () =&gt;
                      {
                          while (!cancellationToken.IsCancellationRequested)
                          {
                              _logger.LogInformation(&quot;Waiting background task {i}&quot;, i);
  
                              var workItem =
                                  await TaskQueue.DequeueAsync(cancellationToken);
  
                              try
                              {
                                  _logger.LogInformation($&quot;Got background task {i}, executing deliveryTag : {workItem.Item1.DeliveryTag} &quot; + DateTime.Now.ToLongTimeString());
                                  await workItem.Item2(cancellationToken, workItem.Item1);
                              }
                              catch (Exception ex)
                              {
                                  _logger.LogError(ex,
                                      &quot;Error occurred executing {WorkItem}.&quot;, nameof(workItem));
                              }
                          }
                      }, _tokenSource.Token);
  
                  _executors[i] = executorTask;
                  executorTask.Start();
              }
  
              return Task.CompletedTask;
          }
  
  </code></pre>
  <p>I'll would like to know why when I increment executorsCount,  the background processing thread that will dequeue and execute the incoming tasks take same times<br />
  if executorsCount = 2 or 8 for example.</p>
  <p>Thanks for help</p>