Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Async Timeout Handling | Timeouts, Parallelism, and Error Handling
C# Async and Await Practice

bookChallenge: Async Timeout Handling

Uppgift

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.

Lösning

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 2
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

close

bookChallenge: Async Timeout Handling

Svep för att visa menyn

Uppgift

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.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 2
single

single

some-alt