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?
You're missing the
T
declaration in the method name. Also youAction<T>
needs to become aFunc<Task<T>, T>
: