Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

653
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda