using System; using System.Threading; using System.Threading.Tasks; namespace S7.Net.Internal { internal class TaskQueue { private static readonly object Sentinel = new object(); private Task prev = Task.FromResult(Sentinel); public async Task Enqueue(Func> action) { var tcs = new TaskCompletionSource(); await Interlocked.Exchange(ref prev, tcs.Task).ConfigureAwait(false); try { return await action.Invoke().ConfigureAwait(false); } finally { tcs.SetResult(Sentinel); } } } }