Files
cefsharp/CefSharp.OffScreen.Example/AsyncContext.cs
T
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

39 lines
963 B
C#

/// https://devblogs.microsoft.com/pfxteam/await-synchronizationcontext-and-console-apps/
///
using System;
using System.Threading;
using System.Threading.Tasks;
namespace CefSharp.OffScreen.Example
{
public static class AsyncContext
{
public static void Run(Func<Task> func)
{
var prevCtx = SynchronizationContext.Current;
try
{
var syncCtx = new SingleThreadSynchronizationContext();
SynchronizationContext.SetSynchronizationContext(syncCtx);
var t = func();
t.ContinueWith(delegate
{
syncCtx.Complete();
}, TaskScheduler.Default);
syncCtx.RunOnCurrentThread();
t.GetAwaiter().GetResult();
}
finally
{
SynchronizationContext.SetSynchronizationContext(prevCtx);
}
}
}
}