-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
333b302
commit 3a4e63a
Showing
2 changed files
with
42 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace CHEF.Extensions | ||
{ | ||
internal static class TaskExtensions | ||
{ | ||
/// <summary> | ||
/// Returns true if the task finished in time, false if the task timed out. | ||
/// </summary> | ||
public static Task<T> WithTimeout<T>(this Task<T> task, TimeSpan timeout, CancellationToken cancellationToken) | ||
{ | ||
return Task.WhenAny(task, Task.Delay(timeout, cancellationToken)).ContinueWith(resultTask => | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
if (resultTask != task) | ||
{ | ||
return default; | ||
} | ||
return task.Result; | ||
}, cancellationToken); | ||
} | ||
} | ||
} |