- Home /
Task.Wait seems to block Unity forever, Task variable is returned from an Async function
I have been playing around with the new available async/await API
I got this simple code
IEnumerator Start()
{
yield return new WaitForSeconds(3f);
var stuffTask = DoStuffAsync();
stuffTask.Wait();
Debug.LogError("Finished waiting as blocking main thread");
}
private async Task DoStuffAsync()
{
Debug.Log("Starting async func " + Time.time);
await Task.Delay(2000);
Debug.Log("Finished waiting 2000 milliseconds " + Time.time);
}
What I would expect is the Unity to freeze for 2 seconds. Instead what happened is it freezes forever. The thing is that if I loop
while(stuffTask.isComplete)
{
yield return null;
}
It seems that the task object is completing and its status is being set to RanToCompletion.
What does Wait() do? why does it not ever return? is it a Unity thing? should I even consider ever using this from the Unity thread?
Comment