public OperationResult CalculateStatistic(long clientId) { Logger.Trace("Recalculate statisctic for client: {0}", clientId); Task.Factory.StartNew(() => { try { var clientAccounts = accountService.GetClientAccounts(clientId); if (!clientAccounts.IsSuccess) { Logger.Error("Error at calculate statistic for client {0}: {1}", clientId, clientAccounts.Error); return; } var accounts = new List <MT4AccountInfo>(); foreach (var account in clientAccounts.Result) { var accInfo = accountService.GetMt4AccountInfo(account.TradingAccountId); if (!accInfo.IsSuccess) { Logger.Error("Error at calculate statistic for client {0}: {1}", clientId, accInfo.Error); continue; } statisticRepository.ClearStatistic(accInfo.Result.AccountId); var accountExist = false; for (var i = 0; i < 5; i++) { if (mt4Repositories[accInfo.Result.ServerName].GetAccount(accInfo.Result.Login) != null) { accountExist = true; break; } Thread.Sleep(2000); } if (!accountExist) { continue; } accounts.Add(accInfo.Result); } statisticManager.CalculateStatisticForAccouts(new Dictionary <long, stat_statistics>(), accounts); } catch (Exception ex) { Logger.Error("Error at calcultate statistic for client {0}: {1}", clientId, ex.ToString()); } }); return(OperationResult.Ok()); }