When best to end a thread?

Hello! :slight_smile: 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.

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.