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.
Oplossing
Bedankt voor je feedback!
single
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you explain this in simpler terms?
What are some examples related to this topic?
Where can I learn more about this?
Geweldig!
Completion tarief verbeterd naar 5.56
Challenge: Async Timeout Handling
Veeg om het menu te tonen
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.
Oplossing
Bedankt voor je feedback!
single