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?