/// https://devblogs.microsoft.com/pfxteam/await-synchronizationcontext-and-console-apps/ using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading; namespace CefSharp.MinimalExample.OffScreen { public sealed class SingleThreadSynchronizationContext : SynchronizationContext { private readonly BlockingCollection> queue = new BlockingCollection>(); public override void Post(SendOrPostCallback d, object state) { queue.Add(new KeyValuePair(d, state)); } public void RunOnCurrentThread() { while (queue.TryTake(out var workItem, Timeout.Infinite)) { workItem.Key(workItem.Value); } } public void Complete() { queue.CompleteAdding(); } } }