Skip to content

Commit

Permalink
Updated the UseBackGroundServicesExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
Simnico99 committed Oct 11, 2022
1 parent 498bae4 commit a9efc9f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Hosting;
using static System.Net.Mime.MediaTypeNames;

namespace ZirconNet.Console.DependencyInjection;
public static class UseBackgroundServicesExtension
Expand All @@ -10,16 +11,16 @@ public static class UseBackgroundServicesExtension
public static IHostBuilder UseBackgroundServices(this IHostBuilder builder)
{
builder.ConfigureServices((_, services) =>
{
foreach (var service in services)
{
foreach (var service in services)
if (service is IHostedService hostedService)
{
if (service is IHostedService hostedService)
{
_taskFactory.StartNew(async () => await hostedService.StartAsync(_cts.Token));
_runningHostedServices.Add(hostedService);
}
_taskFactory.StartNew(() => hostedService.StartAsync(_cts.Token), TaskCreationOptions.RunContinuationsAsynchronously | TaskCreationOptions.LongRunning);
_runningHostedServices.Add(hostedService);
}
});
}
});

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@ public static class UseBackgroundServicesExtension

public static IHostBuilder UseBackgroundServices(this IHostBuilder builder)
{
ThreadDispatcher.Current.Invoke(() => builder.ConfigureServices((_, services) =>
builder.ConfigureServices((_, services) =>
{
foreach (var service in services)
{
ThreadDispatcher.Current.Invoke(() =>
if (service is IHostedService hostedService)
{
foreach (var service in services)
{
if (service is IHostedService hostedService)
{
_taskFactory.StartNew(async () => await hostedService.StartAsync(_cts.Token));
_runningHostedServices.Add(hostedService);
}
}
});
}));
_taskFactory.StartNew(() => hostedService.StartAsync(_cts.Token), TaskCreationOptions.RunContinuationsAsynchronously | TaskCreationOptions.LongRunning);
_runningHostedServices.Add(hostedService);
}
}
});

Application.Current.Exit += CurrentExit;

Expand Down

0 comments on commit a9efc9f

Please sign in to comment.