Challenge: Async Timeout Handling
Compito
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.
Soluzione
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 3. Capitolo 2
single
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Fantastico!
Completion tasso migliorato a 5.56
Challenge: Async Timeout Handling
Scorri per mostrare il menu
Compito
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.
Soluzione
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 3. Capitolo 2
single