- Home /
Question by
FrozenKiwi · Aug 02, 2016 at 06:31 PM ·
augmented-realityaugmented realitymultithreading
async/await in Windows10 universal app
Hi,
I'm using async/await in a Hololens application, however after await operations I consistently see the error message:
ToString can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
It doesn't matter what I am doing, or if the Async task even does any work. Does anyone else have any experience with this, any suggested workarounds?
The code is simply as follows
public class TestAsync : MonoBehaviour {
// Use this for initialization
void Start () {
TestAsyncFunc();
}
// Update is called once per frame
int count = 0;
void Update () {
float sinScale = (float)Math.Sin(0.5f * Time.time);
transform.localScale = new UnityEngine.Vector3(sinScale, sinScale, 1.0f);
Debug.Log("Update: " + count++);
}
private async Task TestAsyncFunc()
{
await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(5));
}
}
Setup a hololens scene (using Holotoolkit-Unity), put this script on an object and put a breakpoint at the end of TestAsyncFunc. The system will print the warning message just before breaking.
While I guess this message is harmless, it could hide much deeper issues (I use await quite a bit) and can cause a lot of lost time in debugging!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612090028im_/https://answers.unity.com/themes/thub/images/avi.jpg)