1

I am trying to update and build a C# project in Unity 5.6.1 to later be run on the Hololens. Originally, the project used System.Threading, but I think that I need to use Tasks instead because of some issues with Unity.

When I open the project in Visual Studio, it runs fine using Tasks. When I build the project in Unity, it says that Tasks do not exist (error below). I am building with .Net 4.6 in Unity on Universal 10 SDK.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace SampleCSApp
{
    // Based on http://answers.unity3d.com/questions/357033/unity3d-and-c-coroutines-vs-threading.html
    class BaseThread
    {
        private bool _isDone = false;
        private object _handle = new object();
        //private System.Threading.Thread _thread = null;
        private System.Threading.Tasks.Task _thread = null;
        private static int WAIT_FOR_MS = 100;

        public bool IsDone
        {
            get
            {
                bool tmp;
                lock (_handle)
                {
                    tmp = _isDone;
                }
                return tmp;
            }
            set
            {
                lock (_handle)
                {
                    _isDone = value;
                }
            }
        }

        public virtual void Start()
        {
            _thread = new System.Threading.Tasks.Task(Run);
            //_thread = new System.Threading.Thread(Run);
            _thread.Start();
        }
        public virtual void Abort()
        {
            _thread.Dispose();
            //_thread.Abort();
        }

        protected virtual void ThreadFunction() { }

        public void WaitFor()
        {
            while (!IsDone)
            {
                _thread.Wait(WAIT_FOR_MS);
                //System.Threading.Thread.Sleep(WAIT_FOR_MS);
            }
        }

        private void Run()
        {
            ThreadFunction();
        }
    }
}

Unity error given:

Assets/SampleCSApp/BaseThread.cs(14,34): error CS0234: The type or namespace name `Tasks' does not exist in the namespace `System.Threading'. Are you missing an assembly reference?

1
  • You should use a existing free threading library from the store, the code example you used is not the greatest. Commented Jun 1, 2017 at 19:21

1 Answer 1

3

Unity is based on .NET 2.0 with some .NET 3.5 features thrown in. Tasks is a .NET 4.0 feature. You need to wait for Unity 2017.1 to be released (or use the beta version) which will support .NET 4.6 via a opt-in option. See this forum post for more details.

We wanted to let everyone on the forum know our future plans for the Mono runtime upgrade.

The Unity 2017.1 release will be available as a public beta soon. In this release, the Mono runtime upgrade feature will be an option. For a given project, you can choose to use the existing version of Mono in Unity (with .NET 3.5 support) or the new Mono runtime (with .NET 4.6 support). In Unity 2017.1, the default setting is to use the older version of Mono. Soon (maybe the 2017.2 release of Unity) we will make the new Mono runtime default, with the old runtime as an option. Later still, we will remove support for the old runtime. More details will be coming in a blog post soon, but we wanted to let everyone on this forum know first.

We really appreciate the time, effort, and feedback so many of you have provided to move this process forward. Our team is focused on shipping the Mono runtime upgrade to all of the Unity users without breaking things. The work you have put in to find many of the things that did break is invaluable.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply, Scott, that makes sense. In Unity Build Settings -> Player Settings -> Other Settings -> Configuration -> Api Compatibility Level there is a .NET 4.6 option. Do you know what that would be used for?
Have not actually played with any of the new scripting stuff yet, so no.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.