Skip to content

Instantly share code, notes, and snippets.

@mycroes
Created July 7, 2018 20:42
Show Gist options
  • Save mycroes/c4f23b056494fa844ad071f1c10782ca to your computer and use it in GitHub Desktop.
Save mycroes/c4f23b056494fa844ad071f1c10782ca to your computer and use it in GitHub Desktop.
ExclusiveTaskQueue
using System;
using System.Threading.Tasks;
namespace Sample
{
internal class ExclusiveTaskQueue
{
private readonly object exclusiveTaskLock = new object();
private Task exclusiveTask = Task.FromResult(true);
public Task Schedule(Func<Task> func)
{
lock (exclusiveTaskLock) return exclusiveTask = exclusiveTask.ContinueWith(t => func()).Unwrap();
}
public Task<TResult> Schedule<TResult>(Func<Task<TResult>> func)
{
lock (exclusiveTaskLock)
{
var res = exclusiveTask.ContinueWith(t => func()).Unwrap();
exclusiveTask = res;
return res;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment