Skip to content

Commit

Permalink
Should have fixed UseBackgroundServices deadlocks once and for all.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simnico99 committed Oct 12, 2022
1 parent ed15df1 commit 28d58da
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ namespace ZirconNet.Console.DependencyInjection;
public static class UseBackgroundServicesExtension
{
private static readonly CancellationTokenSource _cts = new();
private static readonly TaskFactory _taskFactory = new(_cts.Token, TaskCreationOptions.LongRunning, TaskContinuationOptions.None, null);
private static readonly List<IHostedService> _runningHostedServices = new();

public static IHostBuilder UseBackgroundServices(this IHostBuilder builder)
{
builder.ConfigureServices((context, services) =>
builder.ConfigureServices((context, services) => Task.Run(() =>
{
foreach (var service in services)
{
if (service is IHostedService hostedService)
{
_ = Task.Run(() => hostedService.StartAsync(_cts.Token));
hostedService.StartAsync(_cts.Token);
_runningHostedServices.Add(hostedService);
}
}
});
}));

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ namespace ZirconNet.WPF.DependencyInjection;
public static class UseBackgroundServicesExtension
{
private static readonly CancellationTokenSource _cts = new();
private static readonly TaskFactory _taskFactory = new(_cts.Token, TaskCreationOptions.LongRunning, TaskContinuationOptions.None, null);
private static readonly List<IHostedService> _runningHostedServices = new();

public static IHostBuilder UseBackgroundServices(this IHostBuilder builder)
{
builder.ConfigureServices((context, services) =>
builder.ConfigureServices((context, services) => Task.Run(() =>
{
foreach (var service in services)
{
if (service is IHostedService hostedService)
{
_ = Task.Run(() => hostedService.StartAsync(_cts.Token));
hostedService.StartAsync(_cts.Token);
_runningHostedServices.Add(hostedService);
}
}
});
}));

Application.Current.Exit += CurrentExit;
return builder;
Expand Down

0 comments on commit 28d58da

Please sign in to comment.