Files
cefsharp/CefSharp.OffScreen.Example/SingleThreadSynchronizationContext.cs
Alex Maitland 1524482ede Core - Add Cef.InitializeAsync
- InitializeAsyncBrowserProcessHandler is used to complete the taskCompletionSource
  when OnContextInitialized is called
- Update the OffScreen example to be async with a simply SingleThreadSynchronizationContext
  to ensure we continue on our main thread.

Issue #3634
2021-10-22 20:10:38 +10:00

35 lines
1006 B
C#

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