Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

657
Views
How to run simultaneously multiple tasks and wait for them in Unity 2019.4 and .NET 4.X?

I have a method which receives multiple url strings and is supposed to create HTTP requests for each of them, simultaneously, wait for them to finish and then return a list containing the result of each request.

I tried to implement the async/await logic as illustrated with the code below:

private static readonly HttpClient httpClient = new HttpClient();

public class Response
{
    public bool isError;
    public string content;
}

public async void TestManyGetRequestsAsync(List<string> urls)
{
    List<Response> responses = await GetManyRequestsAsync(urls);
    foreach (Response response in responses)
    {
        Debug.LogFormat("Content: {0}", response == null ? "NULL" : response.content);
    }
}

async Task<List<Response>> GetManyRequestsAsync(List<string> urls)
{
    List<Task<Response>> tasks = new List<Task<Response>>();

    foreach (string url in urls)
    {
        Task<Response> task = Task.Run<Response>(() => AsyncGetRequest(url)); // This line appears to do the same thing as the line below
        // Task<RestAPI.Response> task = RestAPI.Get(url);
        tasks.Add(task);
    }

    Response[] responses = await Task.WhenAll(tasks);
    return responses.ToList();
}

public async Task<Response> AsyncGetRequest(string url)
{
    Task<HttpResponseMessage> task = httpClient.GetAsync(url);
    HttpResponseMessage message;

    Debug.LogFormat("Sending GET request for {0}", url);
    try
    {
        message = await task;
    }
    catch
    {
        return null;
    }

    Response response = new Response {
        isError = !message.IsSuccessStatusCode,
        content = await message.Content.ReadAsStringAsync()
    };

    Debug.LogFormat("Received GET response for {0}: {1}", url, response);
    return response;
}

The issue is that every task is running sequentially, meaning that each task is on hold while the previous is in progress.

I have the impression that different factors may be involved (the way I implemented the async/await logic, the way requests are run, etc.) but I am not able to figure out a clear solution to my issue.

Any hint or suggestion would be highly appreciated, thanks!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It might be easier to use Unity's own UnitWebRequest, it's async by nature so all you have to do is start them all, and then check every frame to see if they are done. This simple code below demonstrates this, you can improve performance by using coroutines but it's probably not necessary unless you're handling a very large number of web calls concurrently.

    string[] urls = { "http://url1.com", "http://url2.com" };
    List<UnityWebRequest> webRequests = null;
    List<UnityWebRequest> doneRequests = null;
    bool initialized = false;
    private void Update() {
        InitWebRequests();
        if (CheckWebRequestsDone()) {
            HandleResults(doneRequests);
        }
    }

    private void InitWebRequests() {
        if (initialized) {
            return;
        }

        initialized = true;
        webRequests = new List<UnityWebRequest>();
        foreach (string url in urls) {
            UnityWebRequest wr = UnityWebRequest.Get(url);
            wr.SendWebRequest();
            webRequests.Add(wr);
        }
    }

    private bool CheckWebRequestsDone() {
        foreach(UnityWebRequest wr in webRequests.ToArray()) {
            if (wr.isDone) {
                webRequests.Remove(wr);
                doneRequests.Add(wr);
            }
        }

        return webRequests.Count == 0;
    }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!