/// <summary> /// Executes the action on the UI thread. /// </summary> /// <param name="action">The action to execute.</param> /// <exception cref="System.NotImplementedException"></exception> public virtual void OnUIThread(System.Action action) { if (CheckAccess()) { action(); } else { #if WINDOWS_UWP dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action()).AsTask().Wait(); #else Exception exception = null; System.Action method = () => { try { action(); } catch (Exception ex) { exception = ex; } }; dispatcher.Invoke(method); if (exception != null) { throw new System.Reflection.TargetInvocationException("An error occurred while dispatching a call to the UI Thread", exception); } #endif } }
public async void ReportProgress(int percentProgress, object userState) #endif { if (ProgressChanged != null) { #if NETFX_CORE await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => ProgressChanged(this, new ProgressChangedEventArgs(percentProgress, userState))); #else _dispatcher.Invoke(CoreDispatcherPriority.Normal, (sender, args) => { ProgressChanged(this, new ProgressChangedEventArgs(percentProgress, userState)); }, this, null); #endif } }
/// <summary> /// Executes the action on the UI thread. /// </summary> /// <param name="action">The action to execute.</param> /// <exception cref="System.NotImplementedException"></exception> public void OnUIThread(System.Action action) { if (CheckAccess()) { action(); } else { #if WinRT dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action()).AsTask().Wait(); #elif NET Exception exception = null; System.Action method = () => { try { action(); } catch (Exception ex) { exception = ex; } }; dispatcher.Invoke(method); if (exception != null) { throw new System.Reflection.TargetInvocationException("An error occurred while dispatching a call to the UI Thread", exception); } #else var waitHandle = new System.Threading.ManualResetEvent(false); Exception exception = null; System.Action method = () => { try { action(); } catch (Exception ex) { exception = ex; } waitHandle.Set(); }; dispatcher.BeginInvoke(method); waitHandle.WaitOne(); if (exception != null) { throw new System.Reflection.TargetInvocationException("An error occurred while dispatching a call to the UI Thread", exception); } #endif } }