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

371
Vistas
Why is this different? Async running sequentially vs parallel

Given this:

private static async Task<int> Operation1()
{
    await Task.Delay(1000);
    return 1;
}

private static async Task<int> Operation2()
{
    await Task.Delay(1000);
    return 1;
}

this is running sequentially, i.e. it takes 2 seconds (measured using Stopwatch)

var a = await Operation1();
var b = await Operation2();

var result = Operation3(a, b);

while this takes only 1 second, running things in parallel:

var taskA = Operation1();
var taskB = Operation2();
var a = await taskA;
var b = await taskB;

var result = Operation3(a, b);

Why is that? I would totally assume those 2 are equivalent and produce the same IL.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

In the first case Operation2() doesn't run and start a task until the task returned from Operation1() completes.

In the second case both Operation1() and Operation2() run, creating and starting their tasks before you await completion of the task returned from Operation1()

over 4 years ago · Santiago Trujillo Denunciar

0

In the first example you start Operation1 and wait until it is finished, then you start Operation2.

In the second example, you start Operation1 and then Operation2, after which you wait for Operation1 and then Operation2.

So Operation1 and Operation2 run in parallel in the 2nd example and sequentially in the first.

over 4 years ago · Santiago Trujillo Denunciar

0

The first piece of code is equivalent to this:

var taskA = Operation1();
var a = await taskA;
// At this point the taskA is completed
var taskB = Operation2();
var b = await taskB;

Now it should be obvious why it's not equivalent with the second version:

var taskA = Operation1();
var taskB = Operation2();
// At this point both tasks are started, and probably none is completed
var a = await taskA;
var b = await taskB;

As a side note, one of these two approaches demonstrate a bad practice. Can you guess if it's the first or the second approach? You can find the answer to this quiz in the 1st revision of this answer.

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