i have some C# threading program (a game), that stops with a boolean (as most articles recommends).
while (gameContinueRun)
{
createRound();
line1;
line2;
line3;
line4;
endRound();
}
some code lines lock the game and wait until other thread will release it.
lock (stateSync)
{
Monitor.Wait(stateSync)
}
to stop the thread from another thread i set the boolean to false :
if (cancel)
{
gameContinueRun= false;
}
everything works nice.. but i still need to wait until the end of the current loop (round).
now, i want to end the loop, to break all work in the middle (abort game). another thing is to be able to restrat the loop (open new round). (or in another words, two things : 1. aborting the game 2. cancel the current round and start a new one..)
i thought about it, and got a couple of ways :
unlock all locks, and check after every code line :
if (!cancelRound) { line1; } if (!cancelRound) { line2; } if (!cancelRound) { line3; } if (!cancelRound) { line4; }
not very nice, and very exhausting if one have lots of code lines to cover...
use Thread.Abort(), catching the Exception and resetAbort() if needed.
use goto and labels (which i assume is even uglier then aborting..)
which one is better? or more over, is there a better recommended way? thanks!