title: How to detect and break an endless loop in Unity
body: <p>I am working on a big game in Unity, and one of my methods sometimes goes into an endless loop. For example this might be the code I'm using:</p>
  
  <pre><code>public void veryBigMethod(int n) {
  
      if (n % 2 == 0) {
          while (true) {
              // do a lot of work here
              // that might lead to an infinite loop
          }
      }
      else {
          // much shorter code that never fails
      }
  }
  </code></pre>
  
  <p>I have tried this solution, but it doesn't work:</p>
  
  <pre><code>public void runLoopNeverStuck() {
      Thread t = new Thread(veryBigMethod);
      t.Start();
      if (!t.Join(TimeSpan.FromSeconds(30))) {
          t.Abort();
          throw new Exception("More than 30 secs.");
      }
  }
  </code></pre>
  
  <p>What should I do using only code to detect such a loop and stop it?</p>
  
  <p>Why doesn't my code above work on Unity?</p>
  
  <p><strong><em>Important note</em></strong>
  I can't use Task / async in my code, so please give me answers with threads</p>