- Home /
When best to end a thread?
Hello! :) My code (C#) starts a thread that pings a server every 7 seconds. However, when Unity stops playing... The thread lives on! As I am typing this, Unity is still pinging my server, even though it is not in play mode. Is there any callback I can set, etc, that will be called when Unity is closed so that I can close the thread? My code is NOT a mono-behaviour, and as such, the typical methods such as Update() or Start() are not available.
There must be some function that is called when it sleeps or play ends ... but in any event,
could you just check, in your thread, that the game is still running? If it's not running, have it ter$$anonymous$$ate itself.
Answer by IgnoranceIsBliss · Jan 25, 2012 at 11:04 AM
Why not add a behavior on an empty GameObject (perhaps one marked DontDestroyOnLoad) that can set a static flag (for instance, a boolean) on your thread object when the OnDestroy event occurs - one that the thread can check to see if it's time to shut down?
This detects shutdown and also allows your thread to terminate cleanly.
Your answer
Follow this Question
Related Questions
How to stop a script in c# 2 Answers
open and close a door with a keypress 6 Answers
Door Opening And Closing 2 Answers