SomeGuy
SomeGuy

Reputation: 11

How does QueueBackgroundWorkItem manage threads

Does anyone know how QueueBackgroundWorkItem manages it's threads?

Say, for example, I have a block of code like the following in my API that loops through a list of email addresses and sends an email to each address:

foreach (var address in addresses)
{
    HostingEnvironment.QueueBackgroundWorkItem(async cancellationToken =>
    {
        var emailService = new EmailerService();
        await emailService.SendEmail(cancellationToken, address);
    });
}

In more detail my question is this...will IIS manage the threads created by QueueBackgroundWorkItem on it's own and I don't need to do anything or will this, say if I had 5000+ addresses, create 5000+ threads and kill my app?

Upvotes: 1

Views: 404

Answers (1)

S.N
S.N

Reputation: 5140

It is not IIS manage which manage this but the worker process or run time which serve an execution platform. In this case, ASP.Net run time. The whole process of managing work items ( scheduled worker) done within ASP.NET-managed AppDomain. You don’t need to do anything specific since it is context agnostic and hence once queued, it may eventually execute to complete. The word eventually means a lot here since execution is not guaranteed due to exceptions in logic / cancellations/ app domain shutdown either automatic or explicit.

There is a detailed blog on this topic which will help you to clarify it further.

Upvotes: 1

Related Questions