Lets have this WorkerSample application:
Program.cs
using WorkerSample;
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
})
.Build();
host.Run();
Console.WriteLine("Program terminated");
Worker.cs:
namespace WorkerSample;
public class Worker : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Console.WriteLine("Worker executing ...");
try
{
await Task.Delay(Timeout.Infinite, stoppingToken);
}
catch (OperationCanceledException) { }
try
{
await Task.Delay(25000);
Console.WriteLine("I have shut down gracefully");
}
catch (Exception ex)
{
Console.WriteLine($"Error shutting down gracefully \n {ex.ToString()}");
}
finally
{
Console.WriteLine("Finally block called");
}
}
}
If I run this app on .NET 6.0 and press Ctrl+C:
with await Task.Delay(5000)
I see the following output:
Worker executing ...
I have shut down gracefully
Finally block called
Program terminated
So I have at least 5 seconds for shutting down gracefully.
With await Task.Delay(6000)
I see the following output:
Worker executing ...
Program terminated
So I have less than 6 seconds for shutting down gracefully
In .NET 7.0 things changed. I have al least 25 seconds and less than 30 seconds for shutting down gracefully.
How much time do I have for shutting down gracefully? Does it depend only .NET version? Is it configurable?