コード例 #1
0
        public IHttpActionResult Put()
        {
            var cancellationTokenSource = new CancellationTokenSource();

            var simulationRunner = new SimulationRunner(GameConstants.GridSize, GameConstants.GridSize,
                                                        cancellationTokenSource.Token);

            var simulationRunnerHolder = new SimulationRunnerHolder(simulationRunner, cancellationTokenSource);

            simulationRunnerHolder.LastActiveGet = DateTime.Now;
            simulationRunnerHolder.StartNewRun();

            var gameId = Guid.NewGuid().ToString();

            HttpContext.Current.Application.Add(gameId, simulationRunnerHolder);

            // timer task to check that the research is alive, if not shut it down
            var timer = new System.Timers.Timer();

            timer.Elapsed  += (sender, e) => TimerWorker(sender, e, simulationRunnerHolder, gameId);
            timer.Interval  = 5000;
            timer.Enabled   = true;
            timer.AutoReset = true;
            timer.Start();

            return(Ok(gameId));
        }
コード例 #2
0
        protected void TimerWorker(object sender, ElapsedEventArgs e, SimulationRunnerHolder simulationRunnerHolder, string gameId)
        {
            if (simulationRunnerHolder == null)
            {
                return;
            }

            if (simulationRunnerHolder.LastActiveGet.AddSeconds(5) < DateTime.Now)
            {
                simulationRunnerHolder.CancellationTokenSource.Cancel();
                var timer = (System.Timers.Timer)sender;
                timer.Stop();
            }
        }