Move Task.Run to reduce nesting
MODIFY
WalletWasabi/WabiSabi/Client/CoinJoinManager.cs
WalletWasabi/WabiSabi/Client/CoinJoinManager.cs
@@ -81,7 +81,7 @@ public class CoinJoinManager : BackgroundService
}
// Detects and notifies about wallets that can participate in a coinjoin.
- var walletsMonitoringTask = MonitorWalletsAsync(stoppingToken);
+ var walletsMonitoringTask = Task.Run(() => MonitorWalletsAsync(stoppingToken), stoppingToken);
// Coinjoin handling Start / Stop and finallization.
var coinJoinsTrackerTask = MonitorAndHandleConjoinsAsync(stoppingToken);
@@ -89,8 +89,7 @@ public class CoinJoinManager : BackgroundService
await Task.WhenAll(walletsMonitoringTask, coinJoinsTrackerTask).ConfigureAwait(false);
}
- private Task MonitorWalletsAsync(CancellationToken stoppingToken) =>
- Task.Run(async () =>
+ private async Task MonitorWalletsAsync(CancellationToken stoppingToken)
{
var trackedWallets = new Dictionary<string, Wallet>();
while (!stoppingToken.IsCancellationRequested)
@@ -117,7 +116,7 @@ public class CoinJoinManager : BackgroundService
}
await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken).ConfigureAwait(false);
}
- }, stoppingToken);
+ }
private async Task MonitorAndHandleConjoinsAsync(CancellationToken stoppingToken)
{
@@ -125,14 +124,13 @@ public class CoinJoinManager : BackgroundService
// using a single lock around its access or use a channel.
var trackedCoinJoins = new ConcurrentDictionary<string, CoinJoinTracker>();
- var commandsHandlingTask = HandleCoinJoinCommandsAsync(trackedCoinJoins, stoppingToken);
- var monitorCoinJoinTask = MonitorAndHandlingCoinJoinFinallizationAsync(trackedCoinJoins, stoppingToken);
+ var commandsHandlingTask = Task.Run(() => HandleCoinJoinCommandsAsync(trackedCoinJoins, stoppingToken), stoppingToken);
+ var monitorCoinJoinTask = Task.Run(() => MonitorAndHandlingCoinJoinFinallizationAsync(trackedCoinJoins, stoppingToken), stoppingToken);
await Task.WhenAll(commandsHandlingTask, monitorCoinJoinTask).ConfigureAwait(false);
}
- private Task HandleCoinJoinCommandsAsync(ConcurrentDictionary<string, CoinJoinTracker> trackedCoinJoins, CancellationToken stoppingToken) =>
- Task.Run(async () =>
+ private async Task HandleCoinJoinCommandsAsync(ConcurrentDictionary<string, CoinJoinTracker> trackedCoinJoins, CancellationToken stoppingToken)
{
var coinJoinTrackerFactory = new CoinJoinTrackerFactory(HttpClientFactory, RoundStatusUpdater, stoppingToken);
@@ -180,10 +178,9 @@ public class CoinJoinManager : BackgroundService
break;
}
}
- }, stoppingToken);
+ }
- private Task MonitorAndHandlingCoinJoinFinallizationAsync(ConcurrentDictionary<string, CoinJoinTracker> trackedCoinJoins, CancellationToken stoppingToken) =>
- Task.Run(async () =>
+ private async Task MonitorAndHandlingCoinJoinFinallizationAsync(ConcurrentDictionary<string, CoinJoinTracker> trackedCoinJoins, CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
@@ -205,7 +202,7 @@ public class CoinJoinManager : BackgroundService
await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken).ConfigureAwait(false);
}
- }, stoppingToken);
+ }
private async Task HandleCoinJoinFinalizationAsync(CoinJoinTracker finishedCoinJoin, ConcurrentDictionary<string, CoinJoinTracker> trackedCoinJoins, CancellationToken cancellationToken)
{

Move Task.Run to reduce nesting