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

bookChallenge: Async Timeout Handling

Tehtävä

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.

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you explain this in simpler terms?

What are some examples related to this topic?

Where can I learn more about this?

close

bookChallenge: Async Timeout Handling

Pyyhkäise näyttääksesi valikon

Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 2
single

single

some-alt