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.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain this in simpler terms?
What are some examples related to this topic?
Where can I learn more about this?
Awesome!
Completion rate improved to 5.56
Challenge: Async Timeout Handling
Swipe to show menu
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.
Solution
Thanks for your feedback!
single