private void button2_Click(object sender, RoutedEventArgs e) { if (agent == null) { Environment.Instance.RemovedDirtCount = 0; lastTimeReading = DateTime.Now; agent = new Agent(); Environment env = Environment.Instance; env.AgentAngle = 0; env.AgentPosition = new Point(50, 50); env.SimulationDuration = 60 * 60 * 1000; env.SimulationSpeed = 500; previousPoint = env.AgentPosition; dumpEnvironment(); agentThread = new Thread(new ThreadStart(agent.run)); agentThread.Start(); drawingTimer = new System.Timers.Timer(100); drawingTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); drawingTimer.Enabled = true; } else { drawingTimer.Enabled = false; agentThread.Abort(); } }
private void button2_Click(object sender, RoutedEventArgs e) { agent = new Agent(); Environment env = Environment.Instance; env.AgentAngle = 0; env.AgentPosition = new Point(50, 50); Thread thread = new Thread(new ThreadStart(agent.run)); thread.Start(); System.Timers.Timer t = new System.Timers.Timer(100); t.Elapsed += new ElapsedEventHandler(OnTimedEvent); t.Enabled = true; System.Timers.Timer t2 = new System.Timers.Timer(100); t2.Elapsed += new ElapsedEventHandler(OnTimedEvent2); t2.Enabled = true; }
public Movement(Agent agent) { this.agent = agent; }
private void OnTimedEvent(object source, ElapsedEventArgs e) { DispatcherOperation dispatcherOp = grid1.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate() { elapsedTime += (DateTime.Now - lastTimeReading).TotalMilliseconds * Environment.Instance.SimulationSpeed; lastTimeReading = DateTime.Now; foreach (UIElement elem in tempElements) grid1.Children.Remove(elem); tempElements = new List<UIElement>(); // Line to represent the path Line line = new Line(); line.Stroke = Brushes.DarkOrange; line.X1 = previousPoint.X; line.Y1 = previousPoint.Y; line.X2 = Environment.Instance.AgentPosition.X; line.Y2 = Environment.Instance.AgentPosition.Y; previousPoint = Environment.Instance.AgentPosition; grid1.Children.Add(line); // Circle to represent the agent Ellipse ellipse = new Ellipse(); ellipse.Width = 20; ellipse.Height = 20; ellipse.Stroke = Brushes.Brown; ellipse.Fill = Brushes.Brown; ellipse.HorizontalAlignment = HorizontalAlignment.Left; ellipse.VerticalAlignment = VerticalAlignment.Top; ellipse.Margin = new Thickness(Environment.Instance.AgentPosition.X - 10, Environment.Instance.AgentPosition.Y - 10, 0, 0); tempElements.Add(agent.ShortRangeSensor.getUIElement()); tempElements.AddRange(agent.BumperSensor.getUIElement()); tempElements.Add(ellipse); lock (Environment.Instance.RemovedDirt) { foreach (UIElement elem in Environment.Instance.RemovedDirt) grid1.Children.Remove(elem); Environment.Instance.RemovedDirt.Clear(); } foreach (UIElement elem in tempElements) grid1.Children.Add(elem); label1.Content = String.Format("Elapsed Time : {0:hh\\:mm\\:ss}", TimeSpan.FromMilliseconds(elapsedTime)); if (elapsedTime >= Environment.Instance.SimulationDuration) { agentThread.Abort(); agent = null; elapsedTime = 0; drawingTimer.Enabled = false; // performance using (TextWriter tw = new StreamWriter("F:\\bench.txt", true)) tw.WriteLine(String.Format("{0} / {1}", Environment.Instance.RemovedDirtCount, Environment.Instance.Dirt.Count)); times++; if (times < 25) restart(); } } )); }