I’m attempting to use threads in my Unity project which is being deployed on an Android phone for use with the Google Daydream VR system. I’m having an issue where the thread isn’t dying as I would expect it to.
I’m creating a thread like that seen below, and assigning it a function to run while ‘alive’. When a specific action occurs (in my case, a UDP network goes down), the thread should stop performing and die. However, the thread stops performing its function but isn’t dying.
Thread thread;
private void Update()
{
if (!thread.IsAlive)
{
// Create a new thread.
}
}
public void createNewThread()
{
thread = new Thread(Run);
thread.Priority = System.Threading.ThreadPriority.BelowNormal;
thread.Start();
}
void Run()
{
// Do thread task.
}
In the example above, the thread is created, and runs its task within Run(). When the action occurs, is stops midway through its task within Run() and doesn’t enter again. The Update() function continues to loop, but thread.IsAlive continues to state that the thread is alive, when it’s my understanding it has ceased operation. If I exit the scene that this script is running within, the thread dies and the script continues as expected, but it won’t die while I stay within the scene. I have no idea why.
Almost identical code to this has been tested on a Windows machine, running within Unity, and it works exactly as I expect, which is making me believe this could be an Android/Daydream issue.
Any help in diagnosing what’s going on would be great. It’s hard to post a MWE due to the scale of the code, scenes, and platform required to recreate the issue (sorry).