Created
July 7, 2018 20:42
-
-
Save mycroes/c4f23b056494fa844ad071f1c10782ca to your computer and use it in GitHub Desktop.
ExclusiveTaskQueue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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