- Home /
Threading and Application.isPlaying
So I'm in the editor and I think my threads (which I'm stuck with) are still running even after I hit the stop button.
I'd like to do something like this:
#if UNITY_EDITOR
if (!Application.isPlaying) StopEverything();
#endif
but that Unity interface is only allowed on the "main thread".
So -
a) Am I imagining that the threads still run? I've been pretty convinced at times, and largely am just frustrated by slowness and crashes when working in the editor. But I've not read definitively that the Editor can't deal with shutting down threads that user code has started and would love for someone to (accurately) tell me I'm wrong about that.
b) Given I'm right about that, how do I have these threads shutdown gracefully on their own? If I count on something in the main thread to provide a trigger (OnApplicationQuit() or whatever), I don't feel like I can rely on that since, obviously, I'm debugging code and I can easily mess up something that isn't dead simple.
And if it's relevant, I'm stuck with 3.5 for the time being.
Answer by Ejlersen · Sep 16, 2013 at 08:41 PM
Here is what I usually do, when I need to stop my threads.
• Add a bool to my threads loop. • When I abort my thread, I set the bool to false and check if its aborted properly.
Then you can from some sort of manager script stop the threads from the main thread.
Here is a guide that explains the idea in depth:
http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.90).aspx
That's how these threads get shut down in the normal case. I'm really looking for something simpler than that where no matter how messed up things might be elsewhere in the code, a thread can still stop itself appropriately. Checking Application.isPlaying would be ideal, but alas...
Well, you could keep a static bool on the manager script, which they could check on.
I have the same problem. Unity keeps running my threads even after pressing the pause button. But I can not access the running state of my application from "outside" of my $$anonymous$$ainThread.
Your answer
