WasabiSynchronizer: Constants for state & dispose cleanup.
MODIFY
WalletWasabi/Services/WasabiSynchronizer.cs
WalletWasabi/Services/WasabiSynchronizer.cs
@@ -21,6 +21,14 @@ namespace WalletWasabi.Services
{
public class WasabiSynchronizer : NotifyPropertyChangedBase, IFeeProvider
{
+ private const long StateNotStarted = 0;
+
+ private const long StateRunning = 1;
+
+ private const long StateStopping = 2;
+
+ private const long StateStopped = 3;
+
private decimal _usdExchangeRate;
private AllFeeEstimate _allFeeEstimate;
@@ -30,7 +38,7 @@ namespace WalletWasabi.Services
private BackendStatus _backendStatus;
/// <summary>
- /// 0: Not started, 1: Running, 2: Stopping, 3: Stopped
+ /// Value can be any of: <see cref="StateNotStarted"/>, <see cref="StateRunning"/>, <see cref="StateStopping"/> and <see cref="StateStopped"/>.
/// </summary>
private long _running;
@@ -41,7 +49,7 @@ namespace WalletWasabi.Services
WasabiClient = new WasabiClient(baseUriAction, torSocks5EndPoint);
Network = Guard.NotNull(nameof(network), network);
LastResponse = null;
- _running = 0;
+ _running = StateNotStarted;
Cancel = new CancellationTokenSource();
BitcoinStore = Guard.NotNull(nameof(bitcoinStore), bitcoinStore);
}
@@ -59,7 +67,7 @@ namespace WalletWasabi.Services
public event EventHandler<SynchronizeResponse>? ResponseArrived;
- public SynchronizeResponse LastResponse { get; private set; }
+ public SynchronizeResponse? LastResponse { get; private set; }
public WasabiClient WasabiClient { get; private set; }
@@ -102,7 +110,7 @@ namespace WalletWasabi.Services
public BitcoinStore BitcoinStore { get; private set; }
- public bool IsRunning => Interlocked.Read(ref _running) == 1;
+ public bool IsRunning => Interlocked.Read(ref _running) == StateRunning;
private CancellationTokenSource Cancel { get; set; }
@@ -124,7 +132,7 @@ namespace WalletWasabi.Services
MaxRequestIntervalForMixing = requestInterval; // Let's start with this, it'll be modified from outside.
- if (Interlocked.CompareExchange(ref _running, 1, 0) != 0)
+ if (Interlocked.CompareExchange(ref _running, StateRunning, StateNotStarted) != StateNotStarted)
{
return;
}
@@ -339,7 +347,7 @@ namespace WalletWasabi.Services
}
finally
{
- Interlocked.CompareExchange(ref _running, 3, 2); // If IsStopping, make it stopped.
+ Interlocked.CompareExchange(ref _running, StateStopped, StateStopping); // If IsStopping, make it stopped.
}
});
}
@@ -377,15 +385,15 @@ namespace WalletWasabi.Services
public async Task StopAsync()
{
- Interlocked.CompareExchange(ref _running, 2, 1); // If running, make it stopping.
- Cancel?.Cancel();
- while (Interlocked.CompareExchange(ref _running, 3, 0) == 2)
+ Interlocked.CompareExchange(ref _running, StateStopping, StateRunning); // If running, make it stopping.
+ Cancel.Cancel();
+
+ while (Interlocked.CompareExchange(ref _running, StateStopped, StateNotStarted) == StateStopping)
{
await Task.Delay(50).ConfigureAwait(false);
}
- Cancel?.Dispose();
- Cancel = null;
+ Cancel.Dispose();
WasabiClient?.Dispose();
WasabiClient = null;

WasabiSynchronizer: Constants for state & dispose cleanup.