CefSharp.MinimalExample/CefSharp.MinimalExample.OffScreen/AsyncContext.cs

39 lines
966 B
C#
Raw Normal View History

/// 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);
}
}
}
}