void aiTimer_Elapsed(object sender, ElapsedEventArgs e) { using(IGameDataService gameDataService = new GameDataService()) { IEnumerable<AIAttentionRequiredResult> aiGames = gameDataService.GetAIGamesRequiringAttention(); //If we have any Games that the AI needs to play, loop through them all. //We will notify the AI to play on seperate threads. if (aiGames.Any()) { foreach(var aiGame in aiGames) { BackgroundWorker aiWorker = new BackgroundWorker(); aiWorker.DoWork += aiWorker_DoWork; aiWorker.RunWorkerAsync(aiGame); aiWorker.RunWorkerCompleted += (cs,ce) => { if (ce.Error != null) { Exception ex = ce.Error; while (ex.InnerException != null) { ex = ex.InnerException; } Logger.Instance.Log("CentralServerCommunicationError", string.Format("GameId:{0}|Error:{1}",aiGame.GameId, ex.Message), ce.Error.StackTrace); using (IGameDataService dataService = new GameDataService()) { Match match = dataService.GetMatch(null, aiGame.GameId); CentralServerSession session = dataService.GetCentralServerSession(null, null, aiGame.GameId); Player tttdPlayer = dataService.GetPlayer(match.PlayerOneId); GameConfiguration config = GameConfigCache.Instance.GetConfig(match.MatchId); dataService.EndGame(aiGame.GameId, match.PlayerOneId == aiGame.PlayerId ? match.PlayerTwoId : match.PlayerOneId); dataService.Save(); if (config.GameType == GameType.Network) { MoveRequest challengeRequest = new MoveRequest(); challengeRequest.GameId = session.CentralServerGameId.Value; challengeRequest.PlayerName = tttdPlayer.PlayerName; challengeRequest.X = 0; challengeRequest.Y = 0; challengeRequest.Flags = CentralServerCommunicationChannel.GetStatus(StatusFlag.AcceptLoss); CentralServerCommunicationChannel.Instance.PostMove(challengeRequest, match.CurrentGameId.Value, match.MatchId); } } } }; } } } }