Skip to content

Commit

Permalink
WAF: Add TaskHelper.Run overloads with CancellationToken support
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Jan 16, 2025
1 parent 90bf3c3 commit c70816d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Waf.Foundation;
using System.Waf.UnitTesting;

namespace Test.Waf.Foundation
{
Expand All @@ -16,6 +18,23 @@ public async Task RunTest()
Assert.AreEqual(42, result);
}

[TestMethod]
public void RunWithCancellationTest()
{
using var context = UnitTestSynchronizationContext.Create();

var task = TaskHelper.Run(() => 42, TaskScheduler.FromCurrentSynchronizationContext());
var result = task.GetResult(context);
Assert.AreEqual(42, result);

bool called = false;
var cts = new CancellationTokenSource();
var task2 = TaskHelper.Run(() => called = true, TaskScheduler.FromCurrentSynchronizationContext(), cts.Token);
cts.Cancel();
AssertHelper.ExpectedException<TaskCanceledException>(() => task2.Wait(context));
Assert.IsFalse(called);
}

[TestMethod]
public async Task NoWaitTest()
{
Expand Down
25 changes: 23 additions & 2 deletions src/System.Waf/System.Waf/System.Waf.Core/Foundation/TaskHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class TaskHelper
/// <returns>A task that represents the work queued to execute in the ThreadPool.</returns>
public static Task Run(Action action, TaskScheduler scheduler)
{
return Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.DenyChildAttach, scheduler);
return Run(action, scheduler, CancellationToken.None);
}

/// <summary>Queues the specified work to run on the thread pool and returns a proxy for the task returned by function.</summary>
Expand All @@ -22,7 +22,28 @@ public static Task Run(Action action, TaskScheduler scheduler)
/// <returns>A task that represents the work queued to execute in the ThreadPool.</returns>
public static Task<T> Run<T>(Func<T> action, TaskScheduler scheduler)
{
return Task<T>.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.DenyChildAttach, scheduler);
return Run(action, scheduler, CancellationToken.None);
}

/// <summary>Queues the specified work to run on the thread pool and returns a Task object that represents that work.</summary>
/// <param name="action">The work to execute asynchronously</param>
/// <param name="scheduler">The TaskScheduler that is used to schedule the created Task.</param>
/// <param name="cancellation">The cancellation token that will be assigned to the new task.</param>
/// <returns>A task that represents the work queued to execute in the ThreadPool.</returns>
public static Task Run(Action action, TaskScheduler scheduler, CancellationToken cancellation)
{
return Task.Factory.StartNew(action, cancellation, TaskCreationOptions.DenyChildAttach, scheduler);
}

/// <summary>Queues the specified work to run on the thread pool and returns a proxy for the task returned by function.</summary>
/// <typeparam name="T">The type of the result produced by this task.</typeparam>
/// <param name="action">The work to execute asynchronously</param>
/// <param name="scheduler">The TaskScheduler that is used to schedule the created Task.</param>
/// <param name="cancellation">The cancellation token that will be assigned to the new task.</param>
/// <returns>A task that represents the work queued to execute in the ThreadPool.</returns>
public static Task<T> Run<T>(Func<T> action, TaskScheduler scheduler, CancellationToken cancellation)
{
return Task<T>.Factory.StartNew(action, cancellation, TaskCreationOptions.DenyChildAttach, scheduler);
}

/// <summary>
Expand Down

0 comments on commit c70816d

Please sign in to comment.