[Refactor] Update Task.Run usage to await and discard returned tasks.

Fixes some race conditions.
This commit is contained in:
2025-07-07 22:17:27 +01:00
parent 5096884f2a
commit ca1708a337
14 changed files with 257 additions and 243 deletions

View File

@@ -390,8 +390,8 @@ resharper_loop_can_be_converted_to_query_highlighting
resharper_loop_can_be_partly_converted_to_query_highlighting = warning resharper_loop_can_be_partly_converted_to_query_highlighting = warning
resharper_member_can_be_file_local_highlighting = warning resharper_member_can_be_file_local_highlighting = warning
resharper_member_can_be_internal_highlighting = warning resharper_member_can_be_internal_highlighting = warning
resharper_member_can_be_made_static_global_highlighting = warning resharper_member_can_be_made_static_global_highlighting = none
resharper_member_can_be_made_static_local_highlighting = warning resharper_member_can_be_made_static_local_highlighting = none
resharper_member_can_be_private_global_highlighting = warning resharper_member_can_be_private_global_highlighting = warning
resharper_member_can_be_private_local_highlighting = warning resharper_member_can_be_private_local_highlighting = warning
resharper_member_can_be_protected_global_highlighting = warning resharper_member_can_be_protected_global_highlighting = warning

View File

@@ -59,7 +59,7 @@ public class Vfs : IDisposable
MountPoint = mountPoint MountPoint = mountPoint
}; };
Task.Run(() => _ = Task.Run(() =>
{ {
_fuse.Start(); _fuse.Start();

View File

@@ -498,7 +498,7 @@ public class FileExporter
if(_filesByMachine.Count == 0) if(_filesByMachine.Count == 0)
{ {
_machinePosition++; _machinePosition++;
Task.Run(CompressNextMachine); _ = Task.Run(CompressNextMachine);
return; return;
} }

View File

@@ -58,8 +58,7 @@ public sealed class AboutViewModel : ViewModelBase
Assemblies = []; Assemblies = [];
// TODO: They do not load in time _ = Task.Run(() =>
Task.Run(() =>
{ {
foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies().OrderBy(a => a.FullName)) foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies().OrderBy(a => a.FullName))
{ {

View File

@@ -145,6 +145,6 @@ public sealed class ExportDatViewModel : ViewModelBase
if(!File.Exists(compressedDatPath)) _view.Close(); if(!File.Exists(compressedDatPath)) _view.Close();
Task.Run(() => _worker.DecompressFile(compressedDatPath, _outPath)); _ = Task.Run(() => _worker.DecompressFile(compressedDatPath, _outPath));
} }
} }

View File

@@ -260,6 +260,6 @@ public sealed class ExportRomsViewModel : ViewModelBase
ProgressVisible = true; ProgressVisible = true;
Task.Run(worker.Export); _ = Task.Run(worker.Export);
} }
} }

View File

@@ -216,7 +216,9 @@ public sealed class ImportDatFolderViewModel : ViewModelBase
internal void OnOpened() => RefreshFiles(); internal void OnOpened() => RefreshFiles();
void RefreshFiles() => Task.Run(() => void RefreshFiles()
{
_ = Task.Run(() =>
{ {
Dispatcher.UIThread.Post(() => Dispatcher.UIThread.Post(() =>
{ {
@@ -262,6 +264,7 @@ public sealed class ImportDatFolderViewModel : ViewModelBase
CanStart = true; CanStart = true;
}); });
}); });
}
void ExecuteCloseCommand() => _view.Close(); void ExecuteCloseCommand() => _view.Close();
@@ -307,7 +310,7 @@ public sealed class ImportDatFolderViewModel : ViewModelBase
_worker.SetProgressBounds += OnWorkerOnSetProgressBounds; _worker.SetProgressBounds += OnWorkerOnSetProgressBounds;
_worker.WorkFinished += OnWorkerOnWorkFinished; _worker.WorkFinished += OnWorkerOnWorkFinished;
_worker.RomSetAdded += RomSetAdded; _worker.RomSetAdded += RomSetAdded;
Task.Run(_worker.Import); _ = Task.Run(_worker.Import);
} }
void OnWorkerOnWorkFinished(object sender, MessageEventArgs args) => Dispatcher.UIThread.Post(() => void OnWorkerOnWorkFinished(object sender, MessageEventArgs args) => Dispatcher.UIThread.Post(() =>

View File

@@ -161,7 +161,7 @@ public sealed class ImportDatViewModel : ViewModelBase
{ {
ProgressVisible = true; ProgressVisible = true;
_worker.RomSetAdded += RomSetAdded; _worker.RomSetAdded += RomSetAdded;
Task.Run(_worker.Import); _ = Task.Run(_worker.Import);
} }
public event EventHandler<RomSetEventArgs> RomSetAdded; public event EventHandler<RomSetEventArgs> RomSetAdded;

View File

@@ -244,7 +244,7 @@ public sealed class ImportRomFolderViewModel : ViewModelBase
worker.Finished += OnWorkerOnFinished; worker.Finished += OnWorkerOnFinished;
worker.ImportedRom += OnWorkerOnImportedRom; worker.ImportedRom += OnWorkerOnImportedRom;
Task.Run(() => worker.ProcessPath(FolderPath, true, RecurseArchivesChecked)); _ = Task.Run(() => worker.ProcessPath(FolderPath, true, RecurseArchivesChecked));
} }
void OnWorkerOnImportedRom(object sender, ImportedRomItemEventArgs args) => void OnWorkerOnImportedRom(object sender, ImportedRomItemEventArgs args) =>

View File

@@ -149,7 +149,7 @@ public class MainWindowViewModel : ViewModelBase
{ {
var dialog = new About(); var dialog = new About();
dialog.DataContext = new AboutViewModel(dialog); dialog.DataContext = new AboutViewModel(dialog);
dialog.ShowDialog(_view); _ = dialog.ShowDialog(_view);
} }
async void ExecuteImportDatCommand() async void ExecuteImportDatCommand()

View File

@@ -55,7 +55,9 @@ public sealed class RemoveDatViewModel : ViewModelBase
set => this.RaiseAndSetIfChanged(ref _statusMessage, value); set => this.RaiseAndSetIfChanged(ref _statusMessage, value);
} }
internal void OnOpened() => Task.Run(() => internal void OnOpened()
{
_ = Task.Run(() =>
{ {
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath); using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
@@ -104,3 +106,4 @@ public sealed class RemoveDatViewModel : ViewModelBase
Dispatcher.UIThread.Post(_view.Close); Dispatcher.UIThread.Post(_view.Close);
}); });
} }
}

View File

@@ -146,7 +146,7 @@ public sealed class SettingsViewModel : ViewModelBase
worker.FinishedWithText += CheckUnArFinished; worker.FinishedWithText += CheckUnArFinished;
worker.FailedWithText += CheckUnArFailed; worker.FailedWithText += CheckUnArFailed;
Task.Run(() => worker.CheckUnAr(UnArPath)); _ = Task.Run(() => worker.CheckUnAr(UnArPath));
} }
async void CheckUnArFailed(object sender, ErrorEventArgs args) async void CheckUnArFailed(object sender, ErrorEventArgs args)

View File

@@ -193,7 +193,9 @@ public sealed class SplashWindowViewModel : ViewModelBase
internal void OnOpened() => Dispatcher.UIThread.Post(LoadSettings); internal void OnOpened() => Dispatcher.UIThread.Post(LoadSettings);
void LoadSettings() => Task.Run(() => void LoadSettings()
{
_ = Task.Run(() =>
{ {
try try
{ {
@@ -207,6 +209,7 @@ public sealed class SplashWindowViewModel : ViewModelBase
Dispatcher.UIThread.Post(FailedLoadingSettings); Dispatcher.UIThread.Post(FailedLoadingSettings);
} }
}); });
}
void FailedLoadingSettings() void FailedLoadingSettings()
{ {
@@ -215,7 +218,9 @@ public sealed class SplashWindowViewModel : ViewModelBase
ExitVisible = true; ExitVisible = true;
} }
void CheckUnAr() => Task.Run(() => void CheckUnAr()
{
_ = Task.Run(() =>
{ {
LoadingSettingsUnknown = false; LoadingSettingsUnknown = false;
LoadingSettingsOk = true; LoadingSettingsOk = true;
@@ -233,6 +238,7 @@ public sealed class SplashWindowViewModel : ViewModelBase
Dispatcher.UIThread.Post(FailedCheckUnAr); Dispatcher.UIThread.Post(FailedCheckUnAr);
} }
}); });
}
void FailedCheckUnAr() void FailedCheckUnAr()
{ {
@@ -246,7 +252,7 @@ public sealed class SplashWindowViewModel : ViewModelBase
CheckingUnArUnknown = false; CheckingUnArUnknown = false;
CheckingUnArOk = true; CheckingUnArOk = true;
Task.Run(() => _ = Task.Run(() =>
{ {
try try
{ {
@@ -278,7 +284,7 @@ public sealed class SplashWindowViewModel : ViewModelBase
LoadingDatabaseUnknown = false; LoadingDatabaseUnknown = false;
LoadingDatabaseOk = true; LoadingDatabaseOk = true;
Task.Run(() => _ = Task.Run(() =>
{ {
try try
{ {
@@ -308,7 +314,7 @@ public sealed class SplashWindowViewModel : ViewModelBase
MigratingDatabaseUnknown = false; MigratingDatabaseUnknown = false;
MigratingDatabaseOk = true; MigratingDatabaseOk = true;
Task.Run(() => _ = Task.Run(() =>
{ {
try try
{ {

View File

@@ -128,7 +128,9 @@ public sealed class UpdateStatsViewModel : ViewModelBase
public string CloseLabel => Localization.CloseLabel; public string CloseLabel => Localization.CloseLabel;
public ReactiveCommand<Unit, Unit> CloseCommand { get; } public ReactiveCommand<Unit, Unit> CloseCommand { get; }
internal void OnOpened() => Task.Run(() => internal void OnOpened()
{
_ = Task.Run(() =>
{ {
using var ctx = Context.Create(Settings.Settings.Current.DatabasePath); using var ctx = Context.Create(Settings.Settings.Current.DatabasePath);
@@ -261,6 +263,7 @@ public sealed class UpdateStatsViewModel : ViewModelBase
CanClose = true; CanClose = true;
}); });
}); });
}
void ExecuteCloseCommand() => _view.Close(); void ExecuteCloseCommand() => _view.Close();
} }