Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

661
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda