Google Daydream Thread with Unity

I am trying to use streams in a Unity project that is deploying to an Android phone for use with Google Daydream VR system. I have a problem where the thread does not die as I expected.

I am creating a thread like the one below and assigning a function to it to run in live mode. When a certain action occurs (in my case, the UDP network goes down), the thread should stop executing and die. However, the thread stops executing its function, but does not die.

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 above example, a thread is created and performs its task in Run (). When the action happens, it stops halfway through its task in Run () and doesn't enter again. The Update () function continues to loop, but thread.IsAlive keeps indicating that the thread is alive when I realized that it has stopped running. If I exit the scene this script is running in, the thread dies and the script continues as expected, but it doesn't die while I stay inside the scene. I have no idea why.

Nearly identical code for this has been tested on a Windows machine running in Unity, and it works exactly as I expect, which makes me think it might be an Android / Daydream issue.

Any help diagnosing what's going on would be great. Difficult to post an MWE due to the scale of code, scenes, and platform required to recreate the problem (sorry).

UPDATE: Changed my Windows code to copy Android version even more. It can now be confirmed that this is an Android / Daydream issue and not a scene switching issue. Windows version killed the thread correctly as expected.

0


source to share


2 answers


I would suggest making void Update Public, but it is very difficult to understand what you are trying to do here since we cannot see the code. you can also do while loop with isAlive! = true. But depending on your program, this might not be a good idea.



0


source


First: use IsBackground to tell .Net to turn off the thread when the app exits.

thread = new Thread(Run);
thread.Priority = System.Threading.ThreadPriority.BelowNormal;`
thread.IsBackground = true;
thread.Start();

      

When the thread exits Run in your example, it will die. I'm not sure why you can't see this, but I suggest looking at the code inside Run if something is blocking. A good way to do this is to plug in the Visual Studio debugger, freeze the program and go to Debug-> Windows-> Parallel Stacks. This will give you a visual representation of threads and their stacks.

Starting a thread has a lot of overhead. When idling, the thread has no overhead. In appearance, you could take a different approach. You are basically trying to restart the thread every time it ends.



using UnityEngine;
using System.Threading;

public class Test : MonoBehaviour
{
    private Thread _thread;
    void Start()
    {
        _thread = new Thread(Run);
        _thread.Name = "DaydreamThread";
        _thread.Priority = System.Threading.ThreadPriority.BelowNormal;
        _thread.IsBackground = true;
        _thread.Start();
    }

    private void Run()
    {
        try
        {
            // Endless loop
            for (;;)
            {
                // Do your stuff
            }
        }
        catch (ThreadAbortException)
        {
            // If you expect to do a Abort() on the thread then you want to
            //  ignore this exception
        }
    }
}

      

Alternatively, you can keep the thread running until Update signals it.

using UnityEngine;
using System.Threading;

public class Test : MonoBehaviour
{
    private Thread _thread;
    private ManualResetEvent _signal = new ManualResetEvent(false);
    void Start()
    {
        _thread = new Thread(Run);
        _thread.Name = "DaydreamThread";
        _thread.Priority = System.Threading.ThreadPriority.BelowNormal;
        _thread.IsBackground = true;
        _thread.Start();
    }

    private void Run()
    {
        try
        {
            // Endless loop
            for (;;)
            {
                // Wait for a signal to do another pass
                _signal.WaitOne();
                _signal.Reset();


                // Do your stuff
            }
        }
        catch (ThreadAbortException)
        {
            // If you expect to do a Abort() on the thread then you want to
            //  ignore this exception
        }
    }

    void Update()
    {
        if (something)
            // Signal Run-thread to do another pass
            _signal.Set();
    }
}

      

0


source







All Articles