- Home /
Google Daydream Thread with Unity
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).
Answer by EpsilonQoppa · Jun 30, 2017 at 07:27 PM
Thread.IsAlive can return true even while the thread is not running.
As per msdn:
Property Value Type: System.Boolean true if this thread has been started and has not terminated normally or aborted; otherwise, false.
You can do a couple of things:
if(thread.ThreadState == ThreadState.Running)
or
thread.Abort();
Unfortunately no luck. Both thread.ThreadState and ThreadState.Running poll as 'Running', once I cut the UDP connection triggering the stop of the thread.
Once I leave the scene, and using adb logcat to monitor the log, the thread ends, and ThreadState.Running indicates 'Stopped, AbortRequested'.
As this seems to be a situation where the thread should be working as expected, I'm starting to believe perhaps me abruptly cutting the UDP connection triggering the thread stop is the cause of my issue.
Try thread.Abort() when cutting the UDP connection. This way you know for a fact that is has been aborted.
Thanks again for your help. $$anonymous$$y issue is that there is no way to handle any thread exception, and therefore I can't issue a thread.Abort() as everything just hangs. As soon as the network connection is killed the thread Run() task immediately quits, with no exception, and the Update() function continues to loop while stating the thread is still alive.
I'm starting to believe this problem is caused by an Android networking issue or perhaps my UDP code which is getting spooked when the network suddenly disappears. See my new post here.
UDP is a connectionless protocol. It's a purely packet / message-driven protocol. So there is no way that a connection can be cut. Well the physical connection can be cut, but the protocol has no way to detect this. Even (virtual) connection based protocols like TCP won't detect the loss of connection until it tries to send something and runs into a timeout. UDP does not send / receive acknowledge messages. That's why UDP is an unreliable protocol.
It's of course possible to setup an artifical reliable layer on top of the UDP protocol but without more information on what you actually do inside your Run method we can't really help. Are you sure your thread function actually ends? Try adding a Debug.Log at the end / where you actually leave / ter$$anonymous$$ate the method.
I have try/catches in place to detect when UDP encounters a timeout. I'm unable to put a Debug.Log at the end of the Run method as when the connection is cut (killing power to the server device) the thread will stop randomly in the Run function and won't re-enter. See my other post here for more details
Your answer
Follow this Question
Related Questions
How to using remote Bluetooth button to tap on screen 0 Answers
How to delay gameobject from falling by time? 1 Answer
What version of unity works with the Samsung Gear VR innovator edition for Note 4? 1 Answer
In-App-Purchases not working (Android) 1 Answer
script does not update inspector? 3 Answers