public async Task TryExecuteTopCommand(string clientId) { var topCmd = await _blockchainCommandsRepository.GetTopRecord(clientId); if (topCmd != null) { await _commandConditionsRepository.AttachPendingToCommand(clientId, topCmd.TransactionId); if (await _srvConditionsChecker.AllConditionsMet(topCmd.TransactionId, topCmd.Command.GetCommandType())) { await Sl.WaitAsync(); try { var topCmdToHandle = await _blockchainCommandsRepository.GetTopRecord(clientId); if (topCmdToHandle.TransactionId != topCmd.TransactionId) { return; } if (!topCmdToHandle.DoNotSend) { await _commandSender.SendCommand(topCmd.Command); await _commandConditionsRepository.InsertPendingCondition(clientId, Types.ConfirmationsCondition, new ConfirmationsConditionContext(topCmd.TransactionId).ToJson()); } await _blockchainCommandsRepository.DeleteTopRecord(clientId); if (await _blockchainCommandsRepository.GetTopRecord(clientId) == null) { await _pendingCommandsRepository.Remove(clientId); } } finally { // Release the thread Sl.Release(); } } } }