Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash during auto-update #280

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions LightBulb/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ public App()
{
var services = new ServiceCollection();

// Framework
services.AddSingleton<DialogManager>();
services.AddSingleton<ViewManager>();
services.AddSingleton<ViewModelManager>();

// Services
services.AddSingleton<ExternalApplicationService>();
services.AddSingleton<GammaService>();
services.AddSingleton<HotKeyService>();
services.AddSingleton<SettingsService>();
services.AddSingleton<UpdateService>();

// View model framework
services.AddSingleton<DialogManager>();
services.AddSingleton<ViewModelManager>();

// View models
services.AddTransient<MainViewModel>();
services.AddTransient<DashboardViewModel>();
Expand All @@ -47,9 +48,6 @@ public App()
services.AddTransient<SettingsTabViewModelBase, HotKeySettingsTabViewModel>();
services.AddTransient<SettingsTabViewModelBase, LocationSettingsTabViewModel>();

// View framework
services.AddSingleton<ViewManager>();

_services = services.BuildServiceProvider(true);
_mainViewModel = _services.GetRequiredService<ViewModelManager>().CreateMainViewModel();
}
Expand All @@ -69,12 +67,6 @@ public override void OnFrameworkInitializationCompleted()
Color.Parse("#343838"),
Color.Parse("#F9A825")
);

// Finalize pending updates (and restart) before launching the app
_services.GetRequiredService<UpdateService>().FinalizePendingUpdates();

// Load settings
_services.GetRequiredService<SettingsService>().Load();
}

private void ShowMainWindow()
Expand Down Expand Up @@ -126,7 +118,7 @@ private void DisableTemporarily1MinuteMenuItem_OnClick(object? sender, EventArgs
_mainViewModel.Dashboard.DisableTemporarilyCommand.Execute(TimeSpan.FromMinutes(1));

private void ExitMenuItem_OnClick(object? sender, EventArgs args) =>
ApplicationLifetime?.Shutdown();
ApplicationLifetime?.TryShutdown();

public void Dispose() => _services.Dispose();
}
2 changes: 1 addition & 1 deletion LightBulb/LightBulb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PackageReference Include="Material.Avalonia" Version="3.5.0" />
<PackageReference Include="Material.Icons.Avalonia" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Onova" Version="2.6.10" />
<PackageReference Include="Onova" Version="2.6.11" />
</ItemGroup>

</Project>
4 changes: 3 additions & 1 deletion LightBulb/Services/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public void FinalizePendingUpdates()
return;

_updateManager.LaunchUpdater(lastPreparedUpdate);
Application.Current?.ApplicationLifetime?.Shutdown(2);

if (Application.Current?.ApplicationLifetime?.TryShutdown(2) != true)
Environment.Exit(2);
}
catch (UpdaterAlreadyLaunchedException)
{
Expand Down
12 changes: 5 additions & 7 deletions LightBulb/Utils/Extensions/AvaloniaExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Avalonia.Controls;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;

namespace LightBulb.Utils.Extensions;
Expand All @@ -11,20 +10,19 @@ lifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime
? desktopLifetime.MainWindow
: null;

public static void Shutdown(this IApplicationLifetime lifetime, int exitCode = 0)
public static bool TryShutdown(this IApplicationLifetime lifetime, int exitCode = 0)
{
if (lifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
{
desktopLifetime.TryShutdown(exitCode);
return;
return desktopLifetime.TryShutdown(exitCode);
}

if (lifetime is IControlledApplicationLifetime controlledLifetime)
{
controlledLifetime.Shutdown(exitCode);
return;
return true;
}

Environment.Exit(exitCode);
return false;
}
}
6 changes: 6 additions & 0 deletions LightBulb/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ Click LEARN MORE to find ways that you can help.
[RelayCommand]
private async Task InitializeAsync()
{
// Finalize pending updates (and restart if necessary)
updateService.FinalizePendingUpdates();

// Load settings
settingsService.Load();

await ShowGammaRangePromptAsync();
await ShowFirstTimeExperienceMessageAsync();
await ShowUkraineSupportMessageAsync();
Expand Down