Tasks improvements
MODIFY
WalletWasabi.Fluent/ViewModels/AddWallet/HardwareWallet/HardwareWalletOperations.cs
WalletWasabi.Fluent/ViewModels/AddWallet/HardwareWallet/HardwareWalletOperations.cs
@@ -30,8 +30,10 @@ namespace WalletWasabi.Fluent.ViewModels.AddWallet.HardwareWallet
private CancellationTokenSource DisposeCts { get; }
public Task? DetectionTask { get; set; }
+ private object DetectionTaskLock { get; } = new object();
public Task? InitTask { get; set; }
+ private object InitTaskLock { get; } = new object();
private void OnDetectionCompleted(HwiEnumerateEntry[] wallets)
{
@@ -63,13 +65,16 @@ namespace WalletWasabi.Fluent.ViewModels.AddWallet.HardwareWallet
public void InitHardwareWallet(HwiEnumerateEntry device)
{
- if (InitTask?.Status == TaskStatus.WaitingForActivation)
+ lock (InitTaskLock)
+ {
+ if (InitTask?.IsCompleted is false)
{
return;
}
InitTask = InitHardwareWalletAsync(device);
}
+ }
private async Task InitHardwareWalletAsync(HwiEnumerateEntry device)
{
@@ -91,13 +96,16 @@ namespace WalletWasabi.Fluent.ViewModels.AddWallet.HardwareWallet
public void StartDetection()
{
- if (DetectionTask?.Status == TaskStatus.WaitingForActivation)
+ lock (DetectionTaskLock)
+ {
+ if (DetectionTask?.IsCompleted is false)
{
return;
}
DetectionTask = RunDetectionAsync();
}
+ }
protected async Task RunDetectionAsync()
{
@@ -137,14 +145,28 @@ namespace WalletWasabi.Fluent.ViewModels.AddWallet.HardwareWallet
if (DetectionTask is { } detectionTask)
{
DisposeCts.Cancel();
+ try
+ {
await detectionTask;
+ }
+ catch (Exception ex)
+ {
+ Logger.LogError(ex);
+ }
detectionTask.Dispose();
}
if (InitTask is { } initTask)
{
DisposeCts.Cancel();
+ try
+ {
await initTask;
+ }
+ catch (Exception ex)
+ {
+ Logger.LogError(ex);
+ }
initTask.Dispose();
}

Tasks improvements