Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Async Timeout Handling | Timeouts, Parallelism, and Error Handling
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Async and Await Practice

bookChallenge: Async Timeout Handling

Aufgabe

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ösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 2
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

bookChallenge: Async Timeout Handling

Swipe um das Menü anzuzeigen

Aufgabe

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ösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 2
single

single

some-alt