Note that there are some explanatory texts on larger screens.

plurals
  1. POGame design - main loop aborting - Is there a better way than Abort() and ResetAbort()?
    primarykey
    data
    text
    <p>I have a C# threading program (a game), that stops with a boolean (as most articles recommend).</p> <pre><code>while (gameContinueRun) { createRound(); line1; line2; line3; line4; endRound(); } </code></pre> <p>Some code lines lock the game and wait until other thread will release it:</p> <pre><code>lock (stateSync) { Monitor.Wait(stateSync) } </code></pre> <p>To stop the thread from another thread I set the boolean to false:</p> <pre><code>if (cancel) { gameContinueRun= false; } </code></pre> <p>Everything works nicely, but I still need to wait until the end of the current loop (round).</p> <p>I want to end the loop, to break all work in the middle (abort game). Another thing is to be able to restart the loop (open new round). Or in other words, two things:</p> <ol> <li><p>abort the game</p></li> <li><p>cancel the current round and start a new one</p></li> </ol> <p>I thought about it, and got a couple of ways:</p> <ol> <li><p>Unlock all locks, and check after every code line:</p> <pre><code>if (!cancelRound) { line1; } if (!cancelRound) { line2; } if (!cancelRound) { line3; } if (!cancelRound) { line4; } </code></pre> <p>Not very nice, and very exhausting if one have lots of code lines to cover...</p></li> <li><p>Use <code>Thread.Abort()</code>, catching the <code>Exception</code> and <code>resetAbort()</code> if needed.</p></li> <li><p>Use <code>goto</code> and labels (which I assume is even uglier then aborting).</p></li> </ol> <p>Which way is better? Or moreover, is there a better recommended way?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload