.NET Fiddle

CSharp · Console

/Program.cs

using System;
using System.Threading;
using System.Threading.Tasks;

public static class Program
{
    public static void Main()
    {
        CancellationToken token = new(canceled: true);

        Task taskA = TaskRun2(async () => token.ThrowIfCancellationRequested());
        Task taskB = TaskRun2(async () => token.ThrowIfCancellationRequested(), token);
    
        try { Task.WaitAll(taskA, taskB); } catch { }
    
        Console.WriteLine($"taskA.Status: {taskA.Status}");
        Console.WriteLine($"taskB.Status: {taskB.Status}");
	}

    /// <summary>
    /// Queues the specified work to run on the thread pool, and returns a proxy
    /// for the task returned by the action. The cancellation behavior is the same
    /// with the Task.Run(Action, CancellationToken) method.
    /// </summary>
    public static Task TaskRun2 (Func<Task> action,
        CancellationToken cancellationToken = default)
    {
        ArgumentNullException.ThrowIfNull(action);
        return Task.Run(action, cancellationToken).ContinueWith(t =>
        {
            if (t.IsCanceled)
            {
                CancellationToken taskToken = new TaskCanceledException(t).CancellationToken;
                if (taskToken != cancellationToken)
                    return Task.FromException(new OperationCanceledException(taskToken));
            }
            return t;
        }, CancellationToken.None, TaskContinuationOptions.DenyChildAttach |
            TaskContinuationOptions.ExecuteSynchronously,
            TaskScheduler.Default).Unwrap();
    }

}
;