a5d2bbe145
- Old example is still there for reference, just not used by default - Add AsyncContext/SingleThreadSynchronizationContext to ensure async calls continue on main thread.
39 lines
966 B
C#
39 lines
966 B
C#
/// https://devblogs.microsoft.com/pfxteam/await-synchronizationcontext-and-console-apps/
|
|
|
|
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CefSharp.MinimalExample.OffScreen
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|