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

516
Visualizações
What's the difference between Task.Yield, Task.Run, and ConfigureAwait(false)?

As I understand it, Task.Yield at the beginning of a method will force the caller to continue if it is not awaiting the method. Meanwhile Task.Run and ConfigureAwait(false) both run a Task on a new thread pool thread, which again will force the caller to continue if it is not awaiting the method.

I can't understand the difference between Task.Yield and running a new thread pool thread, since right after it returns to the caller, it will continue executing the rest of the method, which is essentially the same thing.

This post suggests that Yield and Task.Factory.StartNew (which is really just the old version of Task.Run) can be used interchangeably, which seems confusing to me.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Task.Yield is not a replacement for Task.Run and it has nothing to do with Task.ConfigureAwait.

  • Task.Yield - Generates an awaitable that completes just after it is checked for completion.
  • ConfigureAwait(false) - Generates an awaitable from a task that disregards the captured SynchronizationContext.
  • Task.Run - Executes a delegate on a ThreadPool thread.

Task.Yield is different than ConfigureAwait in that it is an awaitable all by itself, and not a configurable wrapper of another awaitable (i.e. the Task). Another difference is that Task.Yield does continue on the captured context.

Task.Run is different then both as it just takes a delegate and runs it on the ThreadPool, you can use it with ConfigureAwait(false) or without.

Task.Yield should be used to force an asynchronous point, not as a replacement to Task.Run. When await is reached in an async method it checks whether the task (or other awaitable) already completed and if so it continues on synchronously. Task.Yield prevents that from happening so it's useful for testing.

Another usage is in UI methods, where you don't want to be hogging the single UI thread, you insert an asynchronous point and the rest is scheduled to be executed in a later time.

over 4 years ago · Santiago Trujillo Relatório

0

Task.Yield continues on the current synchronization context or on the current TaskScheduler if one is present. Task.Run does not do that. It always uses the thread-pool.

For example Task.Yield would stay on the UI thread.

Avoid Task.Yield. It's semantics are less clear. The linked answer is a code smell.

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