Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cancel a dotnet core worker process from code from ExecuteAsync?

So I have a dotnet core worker process, I want to shutdown the worker process on some condition.

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    while (!stoppingToken.IsCancellationRequested)
    {
        _logger.LogInformation("This is a worker process");
        _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

        if (condition == true)
            //do something to basically shutdown the worker process with failure code.
        
        await Task.Delay(2000, stoppingToken);                
    }
}

How can I achieve this? I have tried to break out of the loop but that does not really shutdown the worker process.

//----this does not work-----
//even after doing this, Ctrl-C has to be done to shutdown the worker.
if (condition == true) break; 
like image 777
unlimit Avatar asked Mar 27 '26 19:03

unlimit


1 Answers

Exiting an IHostedService doesn't terminate the application itself. An application may run multiple IHostedService, so it wouldn't make sense for one of them to bring down the entire application.

To terminate the application, the hosted service class must accept an IHostApplicationLifetime instance, and call StopApplication when it wants to terminate the application. The interface will be injected by the DI middleware :

class MyService:BackgroundService
{
    IHostApplicationLifetime _lifetime;

    public MyService(IHostApplicationLifetime lifetime)
    {
        _lifetime=lifetime;
    }
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            ...
            if (condition == true)
            {
                   _lifeTime.StopApplication();
                   return;
            }
                 
            ...            
        }
    }

}

This will signal all other background services to terminate gracefully and cause Run() or RunAsync() to return, thus exiting the application.

Throwing won't end the application either.

The service has to ensure StopApplication is called if it wants the process to terminate, eg :

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    try 
    {
        ...
    }
    catch(Exception exc)
    {
        //log the exception then
        _lifetime.StopApplication();
    }

}
like image 122
Panagiotis Kanavos Avatar answered Mar 29 '26 09:03

Panagiotis Kanavos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!