Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Challenge: Async Timeout Handling | Timeouts, Parallelism, and Error Handling
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Async and Await Practice

bookChallenge: Async Timeout Handling

Tarea

Swipe to start coding

Implement an async timeout handler using Task.WhenAny and CancellationToken. You will create an async method that runs a simulated long-running operation for a specified duration, but cancels it if it takes longer than a specified timeout.

  • The LongRunningOperationAsync method must simulate a long-running operation using Task.Delay, accept the operation duration in milliseconds, and accept a CancellationToken to support cancellation.
  • The RunWithTimeoutAsync method must start the long-running operation (with a configurable duration) and a timeout task, then use Task.WhenAny to determine which completes first.
  • If the long-running operation completes before the timeout, return its result.
  • If the timeout occurs before the operation completes, cancel the operation and return "Operation Timed Out".
  • Both operation duration and timeout must be parameters to allow for flexible testing.

Solución

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you explain this in simpler terms?

What are some examples related to this topic?

Where can I learn more about this?

close

bookChallenge: Async Timeout Handling

Desliza para mostrar el menú

Tarea

Swipe to start coding

Implement an async timeout handler using Task.WhenAny and CancellationToken. You will create an async method that runs a simulated long-running operation for a specified duration, but cancels it if it takes longer than a specified timeout.

  • The LongRunningOperationAsync method must simulate a long-running operation using Task.Delay, accept the operation duration in milliseconds, and accept a CancellationToken to support cancellation.
  • The RunWithTimeoutAsync method must start the long-running operation (with a configurable duration) and a timeout task, then use Task.WhenAny to determine which completes first.
  • If the long-running operation completes before the timeout, return its result.
  • If the timeout occurs before the operation completes, cancel the operation and return "Operation Timed Out".
  • Both operation duration and timeout must be parameters to allow for flexible testing.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2
single

single

some-alt