In my application I have to load a large file and I don’t want it to freeze in the meantime so I’m trying to wrap my head around async/await. I’ve written some dummy code and it works as expected except for the fact that my task’s status always seems to be “WaitingForActivation”:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
public class PointcloudAsyncTest : MonoBehaviour
{
bool isLoading;
bool isLoaded;
TaskStatus taskStatus;
void Awake()
{
Task taskReturn = LoadAsync();
taskStatus = taskReturn.Status;
}
async Task LoadAsync()
{
isLoading = true;
Debug.Log("Starting to load oh yeah!");
await Task.Run(() => {
Task.Delay(15000).Wait();
});
isLoaded = true;
isLoading = false;
}
// Update is called once per frame
void Update()
{
Debug.Log("isLoading " + isLoading);
Debug.Log("isLoaded " + isLoaded);
Debug.Log("TaskStatus " + taskStatus);
}
}
As expected, the LoadAsync method returns control to caller on await without blocking the main thread. After the delay ends isLoaded is set to true, and isLoading is set to false. However, the taskStatus is always WaitingForActivation. I guess I’m misunderstanding how to use the returns from async code, since here there are 2 async code segments (the LoadAsync function itself, and the Task.Run() code). How would I pass the TaskStatus from Task.Run() to the main thread?