private void examToolStripMenuItem_Click(object sender, EventArgs e) { if (!this.examToolStripMenuItem.Enabled) return; __enable_all_menus(false); this.__kill_threads(); System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart((thread) => { if (this.optimalPath == null || this.optimalPath.Count == 0) { this.examToolStripMenuItem.GetCurrentParent().Invoke(new Action(() => { __enable_all_menus(true); this.examToolStripMenuItem.Enabled = false; })); return; } var gh = new GridHelper(this.g); var rand = new Random(); while (this.g.AgentPoint != this.g.GoalPoint) { var l = (List<KeyValuePair<float, GridHelper.Directions>>)this.optimalPath[this.g.AgentPoint]; var s = l[rand.Next(0, l.Count - 1)]; var m = gh.Move(this.g.AgentPoint, s.Value); MarkStartGoalPoints(m.NewPoint, g.GoalPoint); this.DrawDirection(m.OldPoint, s.Value); Application.DoEvents(); System.Threading.Thread.Sleep(500); } MessageBox.Show("Has reached the goal...", "Horray!!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); this.examToolStripMenuItem.GetCurrentParent().Invoke(new Action(() => { __enable_all_menus(true); })); lock (this.ThreadsPool) this.ThreadsPool.Remove(thread as System.Threading.Thread); })); t.Start(t); lock (this.ThreadsPool) this.ThreadsPool.Add(t); }