Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

c# - Wait for all requests to continue

Please, can this code be done async? I have problems because all requests must be terminated before the code can continue, I saw something about how wait an isolated request, but then i couldn't use the loop, I've been trying to solve this for a while, if anyone can help me it would e great.

StartCoroutine(GetTexture());    

IEnumerator GetTexture() {

    for (int i = 0; i < 10; i++)
    {
        string url = string.Format("https://galinha.webhost.com/img/{0}.jpg", words[i]);
        UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
        yield return www.SendWebRequest();

        if(www.isNetworkError || www.isHttpError) {
            selectionSprites.Add(sprites[i]);
        }
        else {
            Texture2D myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
            Sprite spriteFromWeb = Sprite.Create(myTexture, new Rect(0, 0, myTexture.width, myTexture.height), new Vector2(0, 0));
            selectionSprites.Add(spriteFromWeb);  
        }

    }//for
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

can this code be done async?

note that async is a keyword and a bit misleading here. Ofcourse you can do web requests etc async but the code would be completely different since most of the Unity API can't be used on threads...

Coroutines are never async but still run in the main thread like if they were temporary Update methods. In fact the MoveNext call for Coroutines is done right after the Update.


Waiting for multiple WebRequests parallel

So I guess what you rather ment is

can these requests be awaited parallel?

Esiest solution I can imagine where you wouldn't have to change your code structure too much would be to store all UnityWebRequestAsyncOperations (what is returned by SendWebRequest in an array and yield until all of them are isDone like e.g.

using System.Linq;

...

IEnumerator GetTexture() 
{
    var requests = new List<UnityWebRequestAsyncOperation>(10);

    // Start all requests
    for (var i = 0; i < 10; i++)
    {
        var url = string.Format("https://galinha.webhost.com/img/{0}.jpg", words[i]);
        var www = UnityWebRequestTexture.GetTexture(url);

        // starts the request but doesn't wait for it for now
        requests.Add(www.SendWebRequest());
    }

    // Now wait for all requests parallel
    yield return new WaitUntil(() => AllRequestsDone(requests));

    // Now evaluate all results
    HandleAllRequestsWhenFinished(requests);

    foreach(var request in requests)
    {
        request.Dispose();
    }
}

private void HandleAllRequestsWhenFinished(List<UnityWebRequestAsyncOperation> requests)
{
    for(var i = 0; i < 10; i++)
    {
        var www = requests[i].webRequest;
        if(www.isNetworkError || www.isHttpError) 
        {
            selectionSprites.Add(sprites[i]);
        }
        else 
        {
            Texture2D myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
            Sprite spriteFromWeb = Sprite.Create(myTexture, new Rect(0, 0, myTexture.width, myTexture.height), new Vector2(0, 0));
            selectionSprites.Add(spriteFromWeb);  
        }
    }
}

private bool AllRequestsDone(List<UnityWebRequestAsyncOperation> requests)
{
    // A little Linq magic
    // returns true if All requests are done
    return requests.All(r => r.isDone);

    // Ofcourse you could do the same using a loop
    //foreach(var r in requests)
    //{
    //    if(!r.isDone) return false;
    //}
    //return true;
} 

For completeness: WaitUntil and Linq All


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...