Change Interlocked to CancellationToken in autocopies
Related
MODIFY
WalletWasabi.Gui/Controls/WalletExplorer/TransactionViewModel.cs
WalletWasabi.Gui/Controls/WalletExplorer/TransactionViewModel.cs
@@ -15,7 +15,6 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
private TransactionInfo _model;
private bool _clipboardNotificationVisible;
private double _clipboardNotificationOpacity;
- private long _copyNotificationsInprocess = 0;
public TransactionViewModel(TransactionInfo model)
{
@@ -55,37 +54,45 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
set => this.RaiseAndSetIfChanged(ref _clipboardNotificationOpacity, value);
}
+ public CancellationTokenSource CancelClipboardNotification { get; set; }
+
public async Task TryCopyTxIdToClipboardAsync()
{
try
{
+ CancelClipboardNotification?.Cancel();
+ while (CancelClipboardNotification != null)
+ {
+ await Task.Delay(50);
+ }
+ CancelClipboardNotification = new CancellationTokenSource();
+
+ var cancelToken = CancelClipboardNotification.Token;
+
await Application.Current.Clipboard.SetTextAsync(TransactionId);
+ cancelToken.ThrowIfCancellationRequested();
- Interlocked.Increment(ref _copyNotificationsInprocess);
ClipboardNotificationVisible = true;
ClipboardNotificationOpacity = 1;
- try
- {
- await Task.Delay(1000);
- if (Interlocked.Read(ref _copyNotificationsInprocess) <= 1)
- {
+ await Task.Delay(1000, cancelToken);
ClipboardNotificationOpacity = 0;
- await Task.Delay(1000);
- if (Interlocked.Read(ref _copyNotificationsInprocess) <= 1)
- {
+ await Task.Delay(1000, cancelToken);
ClipboardNotificationVisible = false;
}
- }
- }
- finally
+ catch (Exception ex)
{
- Interlocked.Decrement(ref _copyNotificationsInprocess);
+ if (ex is OperationCanceledException || ex is TaskCanceledException)
+ {
+ return;
}
+
+ Logging.Logger.LogWarning<AddressViewModel>(ex);
}
- catch (Exception ex)
+ finally
{
- Logging.Logger.LogWarning<AddressViewModel>(ex);
+ CancelClipboardNotification?.Dispose();
+ CancelClipboardNotification = null;
}
}
}

MODIFY
WalletWasabi.Gui/ViewModels/AddressViewModel.cs
WalletWasabi.Gui/ViewModels/AddressViewModel.cs
@@ -18,7 +18,6 @@ namespace WalletWasabi.Gui.ViewModels
private bool[,] _qrCode;
private bool _clipboardNotificationVisible;
private double _clipboardNotificationOpacity;
- private long _copyNotificationsInprocess = 0;
public HdPubKey Model { get; }
@@ -83,37 +82,45 @@ namespace WalletWasabi.Gui.ViewModels
set => this.RaiseAndSetIfChanged(ref _qrCode, value);
}
+ public CancellationTokenSource CancelClipboardNotification { get; set; }
+
public async Task TryCopyToClipboardAsync()
{
try
{
+ CancelClipboardNotification?.Cancel();
+ while (CancelClipboardNotification != null)
+ {
+ await Task.Delay(50);
+ }
+ CancelClipboardNotification = new CancellationTokenSource();
+
+ var cancelToken = CancelClipboardNotification.Token;
+
await Application.Current.Clipboard.SetTextAsync(Address);
+ cancelToken.ThrowIfCancellationRequested();
- Interlocked.Increment(ref _copyNotificationsInprocess);
ClipboardNotificationVisible = true;
ClipboardNotificationOpacity = 1;
- try
- {
- await Task.Delay(1000);
- if (Interlocked.Read(ref _copyNotificationsInprocess) <= 1)
- {
+ await Task.Delay(1000, cancelToken);
ClipboardNotificationOpacity = 0;
- await Task.Delay(1000);
- if (Interlocked.Read(ref _copyNotificationsInprocess) <= 1)
- {
+ await Task.Delay(1000, cancelToken);
ClipboardNotificationVisible = false;
}
- }
- }
- finally
+ catch (Exception ex)
{
- Interlocked.Decrement(ref _copyNotificationsInprocess);
+ if (ex is OperationCanceledException || ex is TaskCanceledException)
+ {
+ return;
}
+
+ Logging.Logger.LogWarning<AddressViewModel>(ex);
}
- catch (Exception ex)
+ finally
{
- Logging.Logger.LogWarning<AddressViewModel>(ex);
+ CancelClipboardNotification?.Dispose();
+ CancelClipboardNotification = null;
}
}

Change Interlocked to CancellationToken in autocopies
Related https://github.com/zkSNACKs/WalletWasabi/issues/1276