7

.net core BackgroundService or IHostedService's start method is async:

//IHostedService
Task StartAsync(CancellationToken cancellationToken);
//BackgroundService
Task ExecuteAsync(CancellationToken stoppingToken);

So should I write all the logic in the ExecuteAsync/StartAsync method, or should I just start a new thread and return right away?

For example, which of the following two is the correct implementation?

1.

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    new Thread(async () => await DoWork(stoppingToken)).Start();

    await Task.CompletedTask;
}

private async Task DoWork(CancellationToken stoppingToken) 
{
    while (!stoppingToken.IsCancellationRequested)
        //actual works
}

2.

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    while (!stoppingToken.IsCancellationRequested)
    {
        //actual works
        await Task.Delay(1000);//e.g
    }
}

Semantically I think the second one seems to be right, but if there're several IHostedServices, can they run in parallel with the second form?

Edit 1

I also write a sample program that illustrate the hosted services aren't themselves run as seperated threads.

The message "Waiting for signal.." won't be written console until I type a q:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace BackgroundTaskTest
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            var host = new HostBuilder()
                .ConfigureServices((hostContext, services) =>
                {
                    IConfiguration config = hostContext.Configuration;

                    //register tasks
                    services.AddHostedService<ReadService>();
                    services.AddHostedService<BlockService>();
                })
                .UseConsoleLifetime()
                .Build();

            await host.RunAsync();
        }
    }

    public static class WaitClass
    {
        public static AutoResetEvent Event = new AutoResetEvent(false);
    }

    public class ReadService : BackgroundService
    {
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                var c = Console.ReadKey();
                if (c.KeyChar == 'q')
                {
                    Console.WriteLine("\nTrigger event");
                    WaitClass.Event.Set();
                }
                await Task.Delay(1);
            }
        }
    }

    public class BlockService : BackgroundService
    {
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                Console.WriteLine("Waiting for signal..");
                WaitClass.Event.WaitOne();
                Console.WriteLine("Signal waited");
            }
            return Task.CompletedTask;
        }
    }
}
8
  • 1
    Hosted services already run in their own threads. There's no point in firing off a new thread. Jun 10, 2019 at 13:01
  • 2
    @ChrisPratt I used to be thinking that hosted services already run in seperated thread. But I found that if the first service I registered blocks the execution, the following services won't be executed... Jun 11, 2019 at 0:10
  • Perhaps during startup, but not during run. Jun 11, 2019 at 1:01
  • 1
    @ChrisPratt From my test, I assume that the hosted services go to a seperated thread when the first await operation is reached. And that is the behaviour of async operations that it may put those async operations to seperated threads, ,but I think it is not guaranteed that await will put the operation to a seperated thread... Jun 11, 2019 at 1:07
  • 2
    @ChrisPratt , Hosted services are independent, but they are NOT in seperated threads. You have to create your own thread to make them run in seperated threads. Jun 11, 2019 at 4:26

2 Answers 2

10

Just implement it using async/await, as your second example shows. There is no need for an extra Thread.

Side note: Thread should only be used for COM interop; there are far better solutions these days for every other former use case of Thread. As soon as you type new Thread, you already have legacy code.

8
  • Thank you. Do hosted services already run in seperated thread or not? I write my code which registered several services, I found that if my first service block the execution(without a Task.Delay), then following services won't run(there's no printing from the following services). Jun 11, 2019 at 0:12
  • Also could you please explain more about other ways to implement thread function? Do you mean that if I'm writing a thread, I shoulld always change it to a HostedService? Jun 11, 2019 at 0:13
  • 1
    This answer shows a complete misunderstanding of async/await and threads. Just having async and await does not guarantee that your code will run asynchronously nor is Thread obsolete code. It has become more common to use Task.Run where a new Thread was once used but it does not allow all access to all the features of the underlying thread. May 21, 2021 at 8:26
  • 2
    @CraigMiller: I love your comment! It's true that async doesn't necessarily mean asynchronous; since the op used await Task.Delay as their example for work, I assumed they had actual asynchronous work to do. If the implementation is synchronous, I still suggest Task.Run. And I do still believe Thread is obsolete for every scenario except COM interop; I do miss thread names when debugging but that's all. There are more modern solutions that are easier to work with than Thread for every scenario except COM interop. May 21, 2021 at 13:06
  • 4
    @CraigMiller I suggest you look up who you are replying to here before saying the person does not understand async/await and threads.
    – julealgon
    Oct 16, 2021 at 21:01
3

For StartAsync(), at least, you should start a separate thread. If your job is going to take a significant length of time to complete. Otherwise -- at least in my experience, with .NET Core 2.1 on Windows 10 -- the IHostedService won't register as "started" until all the work is done.

I have a test console app that HTTP-GETs the same URL 360 times, waiting 10 seconds between each, and counting how many times it succeeds and how many times it fails. In this app, I have configured ILogger logging with NLog, going both to a text file and to the console. Highest level of logging verbosity for each.

Once the hosted application finishes starting, I see a message logged to the console (and to the file), telling me that the hosted application has started, and that I can press Ctrl+C to quit.

If in StartAsync(), I simply await the test method that does all the work, then I don't see that message logged until all the work is done, over an hour later. If instead, I kick off a new thread as in your first example, then I see the Ctrl+C instructions almost immediately, as I should.

So empirically, your first example appears to be correct.

2
  • StartAsync also does the hostbuilder.Build() command for you. Possibly your log entry is in the wrong position according to this StartAsync? As in, does the main task logging the application has started, or your is that log written within the hosted service? Or even somewhere else? In my case I moved the log about starting the application into the configureservices while creating the hostbuilder (where my logging is configurated, and DI stuff is registering the hosted service). Not a best practice I would say, but hey I'm learning ;)
    – Michael
    Aug 23, 2021 at 18:50
  • 1
    @Michael the log entry with the Ctrl+C instructions does not come from my code at all. It comes from the .NET Core infrastructure (AFAICT). Aug 23, 2021 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.