Challenge: Async Timeout Handling
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
LongRunningOperationAsyncmethod must simulate a long-running operation usingTask.Delay, accept the operation duration in milliseconds, and accept aCancellationTokento support cancellation. - The
RunWithTimeoutAsyncmethod must start the long-running operation (with a configurable duration) and a timeout task, then useTask.WhenAnyto 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
¡Gracias por tus comentarios!
single
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Can you explain this in simpler terms?
What are some examples related to this topic?
Where can I learn more about this?
Genial!
Completion tasa mejorada a 5.56
Challenge: Async Timeout Handling
Desliza para mostrar el menú
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
LongRunningOperationAsyncmethod must simulate a long-running operation usingTask.Delay, accept the operation duration in milliseconds, and accept aCancellationTokento support cancellation. - The
RunWithTimeoutAsyncmethod must start the long-running operation (with a configurable duration) and a timeout task, then useTask.WhenAnyto 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
¡Gracias por tus comentarios!
single