Skip to content

Instantly share code, notes, and snippets.

@LuviKunG
Last active August 21, 2023 07:08
Show Gist options
  • Save LuviKunG/7c9f253f22f5590cd898a8178b3c66fe to your computer and use it in GitHub Desktop.
Save LuviKunG/7c9f253f22f5590cd898a8178b3c66fe to your computer and use it in GitHub Desktop.
C# extension that helps async to callback main thread sync.
using System;
using System.Threading.Tasks;
namespace LuviKunG.Extensions
{
public static class TaskSchedulerExtensions
{
public static Task ContinueWithCurrentSynchronizationContext(this Task task, Action<Task> action)
{
return task.ContinueWith(action, TaskScheduler.FromCurrentSynchronizationContext());
}
public static Task ContinueWithCurrentSynchronizationContext<T>(this Task<T> task, Action<Task<T>> action)
{
return task.ContinueWith(action, TaskScheduler.FromCurrentSynchronizationContext());
}
public static Task<T> ContinueWithCurrentSynchronizationContext<T>(this Task task, Func<Task, T> action)
{
return task.ContinueWith(action, TaskScheduler.FromCurrentSynchronizationContext());
}
public static Task<T> ContinueWithCurrentSynchronizationContext<T>(this Task<T> task, Func<Task, T> action)
{
return task.ContinueWith(action, TaskScheduler.FromCurrentSynchronizationContext());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment