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.
Рішення
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 3. Розділ 2
single
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Чудово!
Completion показник покращився до 5.56
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.
Рішення
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 3. Розділ 2
single