-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidates task cancellation helpers
- Loading branch information
1 parent
a0fa2b7
commit b62fe3e
Showing
5 changed files
with
63 additions
and
64 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
54 changes: 54 additions & 0 deletions
54
src/MsBuildPipeLogger.Server/CancellationTokenExtensions.cs
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,54 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace MsBuildPipeLogger | ||
{ | ||
internal static class CancellationTokenExtensions | ||
{ | ||
public static void Try(this CancellationToken cancellationToken, Action action) => | ||
cancellationToken.Try(action, null); | ||
|
||
public static void Try(this CancellationToken cancellationToken, Action action, Action cancelled) => | ||
cancellationToken.Try<object>(() => | ||
{ | ||
action(); | ||
return null; | ||
}, () => null); | ||
|
||
public static TResult Try<TResult>(this CancellationToken cancellationToken, Func<TResult> action) => | ||
cancellationToken.Try(action, null); | ||
|
||
public static TResult Try<TResult>(this CancellationToken cancellationToken, Func<TResult> action, Func<TResult> cancelled) | ||
{ | ||
if (cancellationToken.IsCancellationRequested) | ||
{ | ||
return cancelled == null ? default(TResult) : cancelled(); | ||
} | ||
try | ||
{ | ||
return action(); | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
// Thrown if the task itself was cancelled from inside the read method | ||
return cancelled == null ? default(TResult) : cancelled(); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
// Thrown if the operation was cancelled (I.e., the task didn't deal with cancellation) | ||
return cancelled == null ? default(TResult) : cancelled(); | ||
} | ||
catch (AggregateException ex) | ||
{ | ||
// Sometimes the cancellation exceptions are thrown in aggregate | ||
if (!(ex.InnerException is TaskCanceledException) | ||
&& !(ex.InnerException is OperationCanceledException)) | ||
{ | ||
throw; | ||
} | ||
return cancelled == null ? default(TResult) : cancelled(); | ||
} | ||
} | ||
} | ||
} |
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
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