/* Constructs a Trains object and triggers simulation */ private void StartSimulation() { /* Constructing a Train object for simulating */ Trains train = new Trains(m, n, vM); SignalConsole.Text = ""; InfoConsole.AppendText("\r\nSignalling Simulation Started!\r\n"); ExpectedTimeLabel.Text = (totalTime / 3600).ToString("00") + ":" + ((totalTime / 60) % 60).ToString("00") + ":" + (totalTime % 60).ToString("00"); /* Constructing a Timer to run the simulator every second */ System.Timers.Timer timer2 = new System.Timers.Timer(intTimer); timer2.Elapsed += (sender, e) => TickEvent2(train, sender, e); timer2.Enabled = true; }
/* Handler for Timer tick event */ private void TickEvent2(Trains train, object sender, ElapsedEventArgs e) { currentTime = e.SignalTime.ToString("HH:mm:ss"); System.Timers.Timer timer = (System.Timers.Timer)sender; counter++; UpdateProgressBar(); if (stop == true) { timer.Stop(); InfoConsole.AppendText("\r\n\r\nSignalling Simulation Stopped!"); train.PrintOnStop(m, this); Stop.Enabled = false; UpdateControls(true); } if (stop == false) { train.Simulate(m, n, counter, gap, currentTime, train, speedTimer, this, vM, vR, sD); finished = train.Finished(m, n, sD); crashed = train.Crashed(m, n, this, sD); /* If the simulation finishes */ if (finished) { timer.Stop(); train.PrintTime(m, this); Stop.Enabled = false; UpdateControls(true); } /* If the simulation fails due to a crash between trains */ if (crashed) { timer.Stop(); Console.Beep(); train.PrintOnStop(m, this); Stop.Enabled = false; UpdateControls(true); } } }