Skip to content

Commit 4ea5d5b

Browse files
authored
Use CancellationToken in AsyncExecution (#1531)
* Use `CancellationToken` in `AsyncExecution` * Review comments * retain original TimeoutException
1 parent 9e9bcc6 commit 4ea5d5b

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/Microsoft.ComponentDetection.Common/AsyncExecution.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ 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);
27-
28-
var completedInTime = await Task.Run(() => work.Wait(timeout));
29-
if (!completedInTime)
26+
var work = toExecute();
27+
try
3028
{
31-
throw new TimeoutException($"The execution did not complete in the allotted time ({timeout.TotalSeconds} seconds) and has been terminated prior to completion");
29+
return await work.WaitAsync(timeout, cancellationToken);
30+
}
31+
catch (TimeoutException ex)
32+
{
33+
throw new TimeoutException($"The execution did not complete in the allotted time ({timeout.TotalSeconds} seconds) and has been terminated prior to completion", ex);
3234
}
33-
34-
return await work;
3535
}
3636

3737
/// <summary>
@@ -43,15 +43,19 @@ public static async Task<T> ExecuteWithTimeoutAsync<T>(Func<Task<T>> toExecute,
4343
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
4444
/// <exception cref="ArgumentNullException">Thrown when <paramref name="toExecute"/> is null.</exception>
4545
/// <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)
46+
public static async Task ExecuteVoidWithTimeoutAsync(Action toExecute, TimeSpan timeout, CancellationToken cancellationToken = default)
4747
{
4848
ArgumentNullException.ThrowIfNull(toExecute);
4949

50-
var work = Task.Run(toExecute, cancellationToken);
51-
var completedInTime = await Task.Run(() => work.Wait(timeout));
52-
if (!completedInTime)
50+
var work = Task.Run(toExecute, CancellationToken.None);
51+
52+
try
53+
{
54+
await work.WaitAsync(timeout, cancellationToken);
55+
}
56+
catch (TimeoutException ex)
5357
{
54-
throw new TimeoutException($"The execution did not complete in the allotted time ({timeout.TotalSeconds} seconds) and has been terminated prior to completion");
58+
throw new TimeoutException($"The execution did not complete in the allotted time ({timeout.TotalSeconds} seconds) and has been terminated prior to completion", ex);
5559
}
5660
}
5761
}

0 commit comments

Comments
 (0)