Skip to content Skip to sidebar Skip to footer

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

Solution 1:

First off: Use IsBackground to tell .Net to shut down the thread when application exits.

thread = newThread(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 are not seeing this, but I suggest looking at the code inside Run if anything is blocking. A good way to do this is to attach Visual Studio debugger, freeze program and go to Debug->Windows->Parallel Stacks. It will give you a visual representation of threads and their stacks.

Starting a thread has a lot of overhead. Idling a thread has near no overhead. From the look of it you could benefit from a different approach. You are basically trying to restart the thread every time it finishes.

using UnityEngine;
using System.Threading;

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

    privatevoidRun()
    {
        try
        {
            // Endless loopfor (;;)
            {
                // 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 Run-thread waiting until Update passes it a signal.

using UnityEngine;
using System.Threading;

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

    privatevoidRun()
    {
        try
        {
            // Endless loopfor (;;)
            {
                // 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
        }
    }

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

Post a Comment for "Google Daydream Thread With Unity"