# QueueBackgroundWorkItem or IHostedService for .NET Core

How to reliably run a resource-intensive process  (sending email, image processing, generating a PDF file) on a background thread?

**QueueBackgroundWorkItem** (QBWI). This was specifically added to enable ASP.NET apps to reliably run short-lived background tasks.

QBWI schedules a task which can run in the background, independent of any request. This differs from a normal ThreadPool work item in that ASP.NET automatically keeps track of how many work items registered through this API are currently running, and the ASP.NET runtime will try to delay AppDomain shutdown until these work items have finished executing.

**IHostedService for .NET Core**\
&#x20;Since .NET Core 2.0, the framework provides a new interface named IHostedService helping you to easily implement hosted services. The basic idea is that you can register multiple background tasks (hosted services), that run in the background while your web host or host is running.

SignalR is one example of an artifact using hosted services, but you can also use it for much simpler things like:

·         A background task polling a database looking for changes

·         A scheduled task updating some cache periodically.

·         An implementation of QueueBackgroundWorkItem.

·         Processing messages from a message queue in the background of a web app while sharing common services such as ILogger.

When you register an IHostedService, .NET Core will call the **StartAsync()** and **StopAsync()** methods of your IHostedService type during application start and stop respectively. Specifically, start is called after the server has started and IApplicationLifetime.ApplicationStarted is triggered.

You could go ahead and create you custom hosted service class from scratch and implement the IHostedService, as you need to do when using .NET Core 2.0.

![](/files/-LjR4zLZmNWQjRNarqz1)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dotnetweb30-ke.gitbook.io/ke/construction-core/concurrency/queuebackgroundworkitem-or-ihostedservice-for-.net-core.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
