Skip to content

Commit

Permalink
Testing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Simnico99 committed Jan 11, 2023
1 parent 3141cd8 commit 655ca5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 42 deletions.
22 changes: 2 additions & 20 deletions src/ZirconNet.Core/Hosting/AddBackgroundServiceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,7 @@ public static IServiceCollection AddBackgroundServices<TImplementation>(this ISe
where TImplementation : BackgroundService
{
_ = services.AddSingleton<TImplementation>();
_ = services.AddSingleton<BackgroundService, TImplementation>(serviceProvider =>
{
var service = serviceProvider.GetRequiredService<TImplementation>();
var hostApplicationLifetime = serviceProvider.GetRequiredService<IHostApplicationLifetime>();

hostApplicationLifetime.ApplicationStarted.Register(() => service.StartAsync(hostApplicationLifetime.ApplicationStopping));
hostApplicationLifetime.ApplicationStopping.Register(async () => await service.StopAsync(hostApplicationLifetime.ApplicationStopped));

return service;
});
_ = services.AddSingleton<BackgroundService, TImplementation>(x => x.GetRequiredService<TImplementation>());
_ = services.AddHostedService(x => x.GetRequiredService<TImplementation>());

return services;
Expand All @@ -42,16 +33,7 @@ public static IServiceCollection AddBackgroundServices<TService, TImplementation
{
_ = services.AddSingleton<TImplementation>();
_ = services.AddSingleton<TService, TImplementation>(x => x.GetRequiredService<TImplementation>());
_ = services.AddSingleton<BackgroundService, TImplementation>(serviceProvider =>
{
var service = serviceProvider.GetRequiredService<TImplementation>();
var hostApplicationLifetime = serviceProvider.GetRequiredService<IHostApplicationLifetime>();

hostApplicationLifetime.ApplicationStarted.Register(() => service.StartAsync(hostApplicationLifetime.ApplicationStopping));
hostApplicationLifetime.ApplicationStopping.Register(async () => await service.StopAsync(hostApplicationLifetime.ApplicationStopped));

return service;
});
_ = services.AddSingleton<BackgroundService, TImplementation>(x => x.GetRequiredService<TImplementation>());
_ = services.AddHostedService(x => x.GetRequiredService<TImplementation>());

return services;
Expand Down
39 changes: 17 additions & 22 deletions src/ZirconNet.WPF/Hosting/Lifetime/UseWpfApplicationLifetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,10 @@ public static IHostBuilder UseWpfApplicationLifetime(this IHostBuilder builder,
/// <param name="window">The <see cref="Window" /> mainwindows to show and wait to close.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the console.</param>
/// <returns>A <see cref="Task"/> that only completes when the token is triggered or shutdown is triggered.</returns>
public static Task RunWpfApplicationAsync<T>(this IHostBuilder builder, CancellationToken cancellationToken = default) where T : Window
public static Task RunWpfApplicationAsync<T>(this IHostBuilder builder, CancellationToken cancellationToken = default) where T : Window
{
builder.ConfigureServices(services => services.AddSingleton<T>());

return Task.Run(async () =>
{
using var host = builder.UseWpfApplicationLifetime().Build();
var window = host.Services.GetRequiredService<T>();

await host.StartAsync(cancellationToken);
await Application.Current.Dispatcher.Invoke(async () => await window.ShowDialogAsync());
await host.StopAsync();
host?.Dispose();
});
builder.UseWpfApplicationLifetime();
return RunWpfApplicationAsyncInternal<T>(builder, cancellationToken);
}

/// <summary>
Expand All @@ -71,18 +61,23 @@ public static Task RunWpfApplicationAsync<T>(this IHostBuilder builder, Cancella
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the console.</param>
/// <returns>A <see cref="Task"/> that only completes when the token is triggered or shutdown is triggered.</returns>
public static Task RunWpfApplicationAsync<T>(this IHostBuilder builder, Action<WpfApplicationLifetimeOptions> configureOptions, CancellationToken cancellationToken = default) where T : Window
{
builder.UseWpfApplicationLifetime(configureOptions);
return RunWpfApplicationAsyncInternal<T>(builder, cancellationToken);
}

private static Task RunWpfApplicationAsyncInternal<T>(IHostBuilder builder, CancellationToken cancellationToken) where T : Window
{
builder.ConfigureServices(services => services.AddSingleton<T>());

return Task.Run(async () =>
{
using var host = builder.UseWpfApplicationLifetime(configureOptions).Build();
var window = host.Services.GetRequiredService<T>();
using var host = builder.Build();
var window = host.Services.GetRequiredService<T>();

await host.StartAsync(cancellationToken);
await Application.Current.Dispatcher.Invoke(async () => await window.ShowDialogAsync());
await host.StopAsync();
host?.Dispose();
});
var startTask = host.StartAsync(cancellationToken);
var dialogTask = window.ShowDialogAsync();
var stopTask = host.StopAsync();
host?.Dispose();

return Task.Factory.ContinueWhenAll(new Task[] { startTask, dialogTask, stopTask }, async (tasks) => { foreach (var task in tasks) { await task; } });
}
}

0 comments on commit 655ca5d

Please sign in to comment.