//using System; //using System.Collections.Generic; //using System.Linq; //using System.Text; //using System.Threading; //using System.Windows.Threading; //namespace NTwain //{ // // from http://blogs.msdn.com/b/pfxteam/archive/2012/01/20/10259049.aspx // /// // /// Provides a pump that supports running asynchronous methods on the current thread. // /// // public static class AsyncPump // { // /// // /// Runs the specified asynchronous function. // /// // /// The asynchronous function to execute. // /// func // /// No task provided. // public static void Run(Func func) // { // if (func == null) throw new ArgumentNullException("func"); // var prevCtx = SynchronizationContext.Current; // try // { // var syncCtx = new DispatcherSynchronizationContext(); // SynchronizationContext.SetSynchronizationContext(syncCtx); // var t = func(); // if (t == null) throw new InvalidOperationException(); // var frame = new DispatcherFrame(); // t.ContinueWith(_ => { frame.Continue = false; }, // TaskScheduler.Default); // Dispatcher.PushFrame(frame); // t.GetAwaiter().GetResult(); // } // finally // { // SynchronizationContext.SetSynchronizationContext(prevCtx); // } // } // } //}