Writing a function asynchronously

I have only one line in my code that I want to run asynchronously because it is an API call and it take some time which freezes my frame with a loading bar when I am using the Vive headset. What is the best way to achive this ? The code is:

public void SendText() { var text = inputTextField.text;

Debug.Log(text);

AIResponse response = apiAiUnity.TextRequest(text);

if (response != null)
{
Debug.Log("Resolved query: " + response.Result.ResolvedQuery);
var outText = JsonConvert.SerializeObject(response, jsonSettings);

Debug.Log("Result: " + outText);

outputField.text = ParseSpeechFromJson(outText); // get the answer from the Json

talkButton.onClick.Invoke(); // Make proxy say the answer

}
else
{
Debug.LogError(“Response is null”);
}

}

and the line that bothers me is AIResponse response = apiAiUnity.TextRequest(text);
I tried putting the function in a coroutine but i get the same results. The API call takes time which freezes the frame of the VR view in the Vive for a second which is totally critical. This is completly unimmersive. Thanks for the help !

[C# async, await Examples - Dot Net Perls

Not sure if this works with json but it works with binary serializing. ](C# async, await Examples - Dot Net Perls)

USE CODE TAGS:

public void SendText() {
    var text = inputTextField.text;

    Debug.Log(text);

    AIResponse response = apiAiUnity.TextRequest(text);

    if (response != null)
    {
        Debug.Log("Resolved query: " + response.Result.ResolvedQuery);
        var outText = JsonConvert.SerializeObject(response, jsonSettings);

        Debug.Log("Result: " + outText);

        outputField.text = ParseSpeechFromJson(outText); // get the answer from the Json

        talkButton.onClick.Invoke(); // Make proxy say the answer

    }
    else
    {
        Debug.LogError("Response is null");
    }

}

Use some sort of threading to do this. There’s a caveat though, you can’t access the unity api while in a thread.

Personally I like using an implementation similar to ‘ThreadNinja’ for this… or just use ThreadNinja, it’s free on the asset store:

There’s also an implementation of Async Task library around the forums (which isn’t default supported in unity, unless using the beta). I’m trying to remember who implemented it, but they have the correct context supplier to support returning the the unity thread.

Though honestly, the ThreadNinja approach works just as fine, and fits into the Coroutine design of unity in general. Allowing for consistent method structures.

Something along the lines of this:

//start as coroutine with threadninja
public IEnumerator SendText()
{
    var text = inputTextField.text;

    Debug.Log(text);

    yield return Ninja.JumpBack;
    AIResponse response = apiAiUnity.TextRequest(text);
    yield return Ninja.JumpToUnity;
   
    if (response != null)
    {
        Debug.Log("Resolved query: " + response.Result.ResolvedQuery);
        var outText = JsonConvert.SerializeObject(response, jsonSettings);

        Debug.Log("Result: " + outText);

        outputField.text = ParseSpeechFromJson(outText); // get the answer from the Json

        talkButton.onClick.Invoke(); // Make proxy say the answer

    }
    else
    {
        Debug.LogError("Response is null");
    }

}

I’ve wrote that corutine before but my problem was it wasn’t multythreaded. Just looked at ThreadNinja and it seems perfect and just what I am looking for. Thank you for your answer

Hi,
tThanks for your replay. I was unaware of ThreadNinja and it seems like this is exac to.
The code you provided seems very logical to me and I was very suprised it didn’t work.
I still got to see the loading Bar while wearing the Vive headset, anyt input?
Thanks !