mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-03 23:12:42 +00:00
1524482ede
- 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
39 lines
963 B
C#
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);
|
|
}
|
|
}
|
|
}
|
|
}
|