I have a main thread to run (ProcessPlayerMoves)
void StartGame()
{
if (mainThread == null)
{
mainThread = new Thread(new ThreadStart(ProcessPlayerMoves));
mainThread.Name = "My Main Thread";
mainThread.IsBackground = true;
mainThread.Start();
}
else
{
}
}
Then the players method is as follows:
private sMove GetMoveForPlayer(Player p)
{
lastMove = null;
playerThread = new Thread(new ParameterizedThreadStart(p.Move));
playerThread.Name = p.Name;
playerThread.Start(game.GameBench);
//Register a listener
p.PlayerMoved += new PlayerMovedHandler(player_PlayerMoved);
// lastMove is assigned in the player_PlayerMoved handler
while (lastMove == null)
;
// if we get here the player moved
//Unregister the listenter
p.PlayerMoved -= player_PlayerMoved;
// Intervel Delay for the main thread
if (chkSimulationDelay.Checked)
Thread.Sleep(simulationInterval);
try
{
playerThread.Join();
}
catch (ThreadAbortException tae)
{
Console.WriteLine(tae.ToString());
}
return p.CurrentMove;
}
During simulation there is no consitentcy, if i want to run say 500 games it just halts at 20 or sometimes 100 or 50. And i do not get an error, the game just stops but the program is still running.