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:
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
}
}
I have tried this solution, but it doesn't work:
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.");
}
}
What should I do using only code to detect such a loop and stop it?
Why doesn't my code above work on Unity?
Important note I can't use Task / async in my code, so please give me answers with threads