Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Instead of using a <code>bool</code> for <code>Running</code>, why not use an Enum with states of <code>Stopped</code>, <code>Starting</code>, <code>Running</code>, and <code>Stopping</code>?</p> <p>That way, you break out of the loop when <code>Running</code> gets set to <code>Stopping</code>, and do your Disposing. Once that's done, <code>Running</code> gets set to <code>Stopped</code>. When OnQuit() sees <code>Running</code> set to <code>Stopped</code>, it will go ahead and exit.</p> <p>Edit: Here's code, quick and dirty, not tested, etc.</p> <pre><code>public class SomeThread : IDisposable { private object locker = new object(); private RunState running = RunState.Stopped; public enum RunState { Stopped, Starting, Running, Stopping, } public RunState Running { get { lock(locker) { return running; } } set { lock(locker) { running = value; } } } public void Run() { while (Running == RunState.Running) { lock(locker) { DoSomething1(); DoSomething2(); } } Dispose(); } private void DoSomething1() { // something awesome happens here } private void DoSomething2() { // something more awesome happens here } public void Dispose() { lock (locker) { Dispose1(); Dispose2(); } Running = RunState.Stopped; } private void Dispose1() { // something awesome happens here } private void Dispose2() { // something more awesome happens here } } public class OtherThread { SomeThread st = new SomeThread(); public void OnQuit() { st.Running = SomeThread.RunState.Stopping; while (st.Running == SomeThread.RunState.Stopping) { // Do something while waiting for the other thread. } Exit(); } } </code></pre>
 

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