How to create a generic Task.ContinueWith extensio

2019-07-24 10:33发布

问题:

So currently I have a Task.ContinueWith extensions method like so:

public static Task ContinueWith_UsingSyncContextWorkaround(this Task task, Action<Task> continuationAction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler, SynchronizationContext sc)
{
    Action<Task> actionWithWorkaround = t =>
    {
        SynchronizationContext.SetSynchronizationContext(sc);
        continuationAction(t);
    };

    return task.ContinueWith(actionWithWorkaround, cancellationToken, continuationOptions, scheduler);
}

and this works fine but it doesn't give me access to the result of the Task so I would like to create a generic extension method.

I have tried:

public static Task<T> ContinueWith_UsingSyncContextWorkaround(this Task<T> task, Action<Task<T>> continuationAction, CancellationToken cancellationToken, TaskContinuationOptions continuationOptions, TaskScheduler scheduler, SynchronizationContext sc)
{
    Action<Task<T>> actionWithWorkaround = t =>
    {
        SynchronizationContext.SetSynchronizationContext(sc);
        continuationAction(t);
    };

    return task.ContinueWith(actionWithWorkaround, cancellationToken, continuationOptions, scheduler);
}

But this won't compile because

The type or namespace name T could not be found

How do I create a generic Task.ContinueWith extension method?

回答1:

You're missing the T declaration in the method name. Also you Action<T> needs to become a Func<Task<T>, T>:

public static Task<T> ContinueWithUsingSyncContextWorkaround<T>(this Task<T> task,
                                               Func<Task<T>, T> continuationFunc, 
                                               CancellationToken cancellationToken,
                                               TaskContinuationOptions continuationOptions,
                                               TaskScheduler scheduler,
                                               SynchronizationContext sc)
{
    Func<Task<T>, T> funcWithWorkaround = t =>
    {
        SynchronizationContext.SetSynchronizationContext(sc);
        return continuationFunc(t);
    };

    return task.ContinueWith<T>(funcWithWorkaround, cancellationToken,
                                continuationOptions, scheduler);
}