Skip to content

Commit 7b8e8e6

Browse files
committed
Use CancellationToken in AsyncExecution
1 parent 58a3de5 commit 7b8e8e6

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/Microsoft.ComponentDetection.Common/AsyncExecution.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,23 @@ public static class AsyncExecution
1919
/// <returns>The result of the function.</returns>
2020
/// <exception cref="ArgumentNullException">Thrown when <paramref name="toExecute"/> is null.</exception>
2121
/// <exception cref="TimeoutException">Thrown when the execution does not complete within the timeout.</exception>
22-
public static async Task<T> ExecuteWithTimeoutAsync<T>(Func<Task<T>> toExecute, TimeSpan timeout, CancellationToken cancellationToken)
22+
public static async Task<T> ExecuteWithTimeoutAsync<T>(Func<Task<T>> toExecute, TimeSpan timeout, CancellationToken cancellationToken = default)
2323
{
2424
ArgumentNullException.ThrowIfNull(toExecute);
2525

26-
var work = Task.Run(toExecute);
26+
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
27+
cts.CancelAfter(timeout);
2728

28-
var completedInTime = await Task.Run(() => work.Wait(timeout));
29-
if (!completedInTime)
29+
var work = Task.Run(toExecute, cts.Token);
30+
31+
try
32+
{
33+
return await work;
34+
}
35+
catch (OperationCanceledException) when (cts.IsCancellationRequested && !cancellationToken.IsCancellationRequested)
3036
{
3137
throw new TimeoutException($"The execution did not complete in the allotted time ({timeout.TotalSeconds} seconds) and has been terminated prior to completion");
3238
}
33-
34-
return await work;
3539
}
3640

3741
/// <summary>
@@ -43,13 +47,20 @@ public static async Task<T> ExecuteWithTimeoutAsync<T>(Func<Task<T>> toExecute,
4347
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
4448
/// <exception cref="ArgumentNullException">Thrown when <paramref name="toExecute"/> is null.</exception>
4549
/// <exception cref="TimeoutException">Thrown when the execution does not complete within the timeout.</exception>
46-
public static async Task ExecuteVoidWithTimeoutAsync(Action toExecute, TimeSpan timeout, CancellationToken cancellationToken)
50+
public static async Task ExecuteVoidWithTimeoutAsync(Action toExecute, TimeSpan timeout, CancellationToken cancellationToken = default)
4751
{
4852
ArgumentNullException.ThrowIfNull(toExecute);
4953

50-
var work = Task.Run(toExecute, cancellationToken);
51-
var completedInTime = await Task.Run(() => work.Wait(timeout));
52-
if (!completedInTime)
54+
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
55+
cts.CancelAfter(timeout);
56+
57+
var work = Task.Run(toExecute, cts.Token);
58+
59+
try
60+
{
61+
await work;
62+
}
63+
catch (OperationCanceledException) when (cts.IsCancellationRequested && !cancellationToken.IsCancellationRequested)
5364
{
5465
throw new TimeoutException($"The execution did not complete in the allotted time ({timeout.TotalSeconds} seconds) and has been terminated prior to completion");
5566
}

0 commit comments

Comments
 (0)