// ReSharper disable once UnusedMember.Global public static void RunSync(Func <Task> task) { var oldContext = SynchronizationContext.Current; var synch = new ExclusiveSynchronizationContext(); SynchronizationContext.SetSynchronizationContext(synch); synch.Post(async _ => { try { await task(); } catch (Exception e) { synch.InnerException = e; throw; } finally { synch.EndMessageLoop(); } }, null !); synch.BeginMessageLoop(); SynchronizationContext.SetSynchronizationContext(oldContext); }
// ReSharper disable once UnusedMember.Global public static T RunSync <T>(Func <Task <T> > task) { var oldContext = SynchronizationContext.Current; var synch = new ExclusiveSynchronizationContext(); SynchronizationContext.SetSynchronizationContext(synch); var ret = default(T); synch.Post(async _ => { try { ret = await task(); } catch (Exception e) { synch.InnerException = e; throw; } finally { synch.EndMessageLoop(); } }, null !); synch.BeginMessageLoop(); SynchronizationContext.SetSynchronizationContext(oldContext); return(ret !); }