public static void NecessityInvoke(this Dispatcher self, Action action) { if (self.CheckAccess()) action(); self.Invoke(action); }
public static void adoptAsync(this Dispatcher dispatcher, Action del) { if(dispatcher.CheckAccess()) del(); else dispatcher.BeginInvoke(del); }
public static void InvokeIfRequired(this Dispatcher dispatcher, Action action, DispatcherPriority priority) { if (!dispatcher.CheckAccess()) dispatcher.Invoke(priority, (Delegate) action); else action(); }
/// <summary> /// Executes an action on the UI thread, waiting for the action to complete.<br/> /// If this method is called from the UI thread, the action is executed immediately.<br/> /// If the method is called from another thread, the action will be enqueued on the UI thread's dispatcher /// and executed synchronously. /// </summary> /// <param name="dispatcher">Dispatcher instance.</param> /// <param name="action">Action to execute.</param> /// <exception cref="ArgumentNullException"> /// <paramref name="dispatcher"/> is <see langword="null"/>. /// <para>-or-</para> /// <paramref name="action"/> is <see langword="null"/>. /// </exception> public static void InvokeOnUIThread(this Dispatcher dispatcher, Action action) { if (dispatcher == null) { throw new ArgumentNullException("dispatcher"); } if (action == null) { throw new ArgumentNullException("action"); } if (dispatcher.CheckAccess()) { action(); } else { using (ManualResetEventSlim resetEvent = new ManualResetEventSlim(false)) { dispatcher.BeginInvoke(() => { action(); resetEvent.Set(); }); resetEvent.Wait(); } } }
/// <summary> /// Determines whether the calling thread has access to this Dispatcher. /// </summary> /// <param name="dispatcher"></param> /// <remarks>Only the thread the Dispatcher is created on may access the Dispatcher. /// This method is public; therefore, any thread can check to see whether it has access to the Dispatcher. /// The difference between CheckAccess and VerifyAccess is CheckAccess returns a Boolean if the calling thread does /// not have access to the Dispatcher and VerifyAccess throws an exception. ///</remarks> public static void VerifyAccess(this Dispatcher dispatcher) { if (!dispatcher.CheckAccess()) { throw new InvalidOperationException("Cross-thread operation not valid"); } }
public static void SyncInvoke(this Dispatcher dispatcher, Action action) { if (dispatcher.CheckAccess()) action(); else dispatcher.Invoke(action); }
/// <summary> /// Executes the specified action asynchronously on the thread the Dispatcher is associated with, after the specified timeout. /// </summary> /// <param name="dispatcher">The dispatcher instance.</param> /// <param name="timeout">The <see cref="TimeSpan"/> representing the amount of time to delay before the action is invoked.</param> /// <param name="action">The <see cref="Action"/> to execute.</param> public static void BeginInvokeAfterTimeout(this Dispatcher dispatcher, TimeSpan timeout, Action action) { if (!dispatcher.CheckAccess()) { dispatcher.BeginInvoke(() => dispatcher.BeginInvokeAfterTimeout(timeout, action)); } else { var dispatcherTimer = new DispatcherTimer() { Interval = timeout }; dispatcherTimer.Tick += (s, e) => { dispatcherTimer.Stop(); dispatcherTimer = null; action(); }; dispatcherTimer.Start(); } }
public static object GetPropertyValue(this DependencyObject o, DependencyProperty prop) { if (o.CheckAccess()) return o.GetValue(prop); return o.Dispatcher.Invoke(new Func<DependencyObject, DependencyProperty, object>(GetPropertyValue), o, prop); }
public static void InvokeOrExecute(this Dispatcher dispatcher, Action action) { if (dispatcher.CheckAccess()) { action(); } else { dispatcher.BeginInvoke(DispatcherPriority.Normal, action); } }
private static Task DoAsync(this Dispatcher dispatcher, Action action) { if (!dispatcher.CheckAccess()) { return RunAsync(dispatcher, action); } return RunSynchronously(action); }
public static bool CheckAccess(this IAuthorizationService authorizationService, IPrincipal principal, Operation operation, Resource resource) { var claimsPrincipal = principal.AsClaimsPrincipal(); var resources = new Collection<Claim>(new Claim[] { resource }.ToList()); var operations = new Collection<Claim>(new Claim[] { operation }.ToList()); var authorizationContext = new AuthorizationContext(claimsPrincipal, resources, operations); return authorizationService.CheckAccess(authorizationContext); }
public static void SetPropertyValue(this DependencyObject o, DependencyProperty prop, object value) { if (o.CheckAccess()) o.SetValue(prop, value); else o.Dispatcher.Invoke(new Action<DependencyObject, DependencyProperty, object>(SetPropertyValue), o, prop, value); }
private static void Do(this Dispatcher dispatcher, Action action) { if (!dispatcher.CheckAccess()) { dispatcher.BeginInvoke(action, DispatcherPriority.Background); return; } action(); }
/// <summary> /// Invokes the specified action asynchronously using the <see cref="Dispatcher"/> if required. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="action">The action.</param> /// <returns>A task containing the invoked action.</returns> public static async Task RunAsync(this Dispatcher dispatcher, Action action) { if (dispatcher.CheckAccess()) { action(); } else { await dispatcher.InvokeAsync(() => RunAsync(dispatcher, action)); } }
public static void SmartBeginInvoke(this Dispatcher dispatcher, Action a) { if (dispatcher.CheckAccess()) { a.Invoke(); } else { dispatcher.BeginInvoke(a); } }
public static void InvokeActionSafe(this Dispatcher dispatcher, Action action) { if (dispatcher.CheckAccess()) { action.Invoke(); } else { dispatcher.Invoke(action); } }
public static void InvokeIfNecessary(this Dispatcher dispatcher, Action action) { if (dispatcher.CheckAccess()) { action(); } else { dispatcher.Invoke(action); } }
public static void CheckedInvoke(this Dispatcher dispatcher, Action action, bool async = false) { if (dispatcher == null || action == null) return; if (dispatcher.CheckAccess()) action(); else if (async) dispatcher.BeginInvoke(action); else dispatcher.Invoke(action, new object[0]); }
/// <summary> /// A simple WPF threading extension method, to invoke a delegate /// on the correct thread if it is not currently on the correct thread /// Which can be used with DispatcherObject types /// </summary> /// <param name="disp">The Dispatcher object on which to do the Invoke</param> /// <param name="dotIt">The delegate to run</param> /// <param name="priority">The DispatcherPriority</param> public static void InvokeIfRequired(this Dispatcher disp, Action action, DispatcherPriority priority) { if (disp.CheckAccess()) { action(); } else { disp.Invoke(action, priority); } }
/// <summary> /// A simple threading extension method, to invoke a delegate /// on the correct thread if it is not currently on the correct thread /// which can be used with DispatcherObject types. /// </summary> /// <param name="dispatcher">The Dispatcher object on which to /// perform the Invoke</param> /// <param name="action">The delegate to run</param> public static void InvokeIfRequired(this Dispatcher dispatcher, Action action) { if (!dispatcher.CheckAccess()) { dispatcher.Invoke(DispatcherPriority.Normal, action); } else { action(); } }
public static void adopt(this Dispatcher dispatcher, Action del) { try { if (dispatcher.CheckAccess()) del(); else dispatcher.Invoke(del); } catch (TaskCanceledException) { //Application was closing. } }
/// <summary> /// Dispatcher ExtensionMethod to Invoke only if required. /// </summary> public static void InvokeIfRequired(this Dispatcher dispatcher, Action action) { if (dispatcher != null && !dispatcher.IsDisposed) { if (dispatcher.CheckAccess()) { action(); } else { dispatcher.Invoke(action); } } }
public static void AddItem(this ListBox lb, string text, int index, bool waitUntilReturn = false) { var lbit = new ListBoxItem(); lbit.Content = text; lbit.Tag = index; Action additem = () => lb.Items.Add(lbit); if (lb.CheckAccess()) { additem(); } else if (waitUntilReturn) { lb.Dispatcher.Invoke(additem); } else { lb.Dispatcher.BeginInvoke(additem); } }
public static void RemoveItem(this ListBox lb, string text, int index, bool waitUntilReturn = false) { //ListBoxItem lbit; //lbit.Content = text; //lbit.Tag = index; Action additem = () => lb.Items.Remove(""); if (lb.CheckAccess()) { additem(); } else if (waitUntilReturn) { lb.Dispatcher.Invoke(additem); } else { lb.Dispatcher.BeginInvoke(additem); } }
/// <summary> /// Support Invoke of an Action directly so we don't have to have all that ugliness as seen in this function /// </summary> /// <param name="dispatcher"></param> /// <param name="action"></param> public static void Invoke(this Dispatcher dispatcher, Action action) { AutoResetEvent done = new AutoResetEvent(false); if (dispatcher.CheckAccess()) { action(); } else { dispatcher.BeginInvoke((Action)(() => { action(); done.Set(); })); done.WaitOne(); } }
/// <summary> /// Executes the specified delegate on the dispatcher thread. /// </summary> /// <param name="dispatcher"> /// The dispatcher. /// </param> /// <param name="action"> /// The action. /// </param> public static void InvokeOnDispatcherThread(this Dispatcher dispatcher, Action action) { if (dispatcher == null) throw new ArgumentNullException("dispatcher"); if (action == null) throw new ArgumentNullException("action"); if (dispatcher.CheckAccess()) { action(); } else { dispatcher.BeginInvoke(action); } }
/// <summary> /// Executes the specified delegate with the specified arguments synchronously on the thread the Dispatcher is associated with. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="method">A delegate to a method that takes parameters specified in args, which is pushed onto the Dispatcher event queue.</param> /// <param name="args">An array of objects to pass as arguments to the given method. Can be <c>null</c>.</param> /// <exception cref="ArgumentNullException">The <paramref name="method" /> is <c>null</c>.</exception> /// <remarks>For target frameworks where the <see cref="Dispatcher" /> class does not contain the <c>Invoke</c> method, the <c>BeginInvoke</c> /// method will be used instead.</remarks> public static void Invoke(this Dispatcher dispatcher, Delegate method, params object[] args) { Argument.IsNotNull("method", method); if (dispatcher != null && !dispatcher.CheckAccess()) { #if NET dispatcher.Invoke(method, args); #elif NETFX_CORE dispatcher.BeginInvoke(() => method.DynamicInvoke(args)); #else dispatcher.BeginInvoke(method, args); #endif } else { method.DynamicInvoke(args); } }
/// <summary> /// Executes the specified action with the specified arguments synchronously on the thread the Dispatcher is associated with. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="action">The action.</param> /// <exception cref="ArgumentNullException">The <paramref name="action" /> is <c>null</c>.</exception> /// <remarks>For target frameworks where the <see cref="Dispatcher" /> class does not contain the <c>Invoke</c> method, the <c>BeginInvoke</c> /// method will be used instead.</remarks> public static void Invoke(this Dispatcher dispatcher, Action action) { Argument.IsNotNull("action", action); if (dispatcher != null && !dispatcher.CheckAccess()) { #if NET dispatcher.Invoke(action, null); #elif NETFX_CORE dispatcher.BeginInvoke(action); #else dispatcher.BeginInvoke(action); #endif } else { action.Invoke(); } }
/// <summary> /// Executes the specified delegate asynchronously with the specified arguments on the thread that the Dispatcher was created on. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="action">The action.</param> /// <param name="onlyBeginInvokeWhenNoAccess">If set to <c>true</c>, the action will be executed directly if possible. Otherwise, /// <c>Dispatcher.BeginInvoke</c> will be used.</param> public static void BeginInvoke(this Dispatcher dispatcher, Action action, bool onlyBeginInvokeWhenNoAccess) { Argument.IsNotNull("action", action); bool actionInvoked = false; if (dispatcher != null) { if (!onlyBeginInvokeWhenNoAccess || !dispatcher.CheckAccess()) { #if NETFX_CORE dispatcher.BeginInvoke(action); #else dispatcher.BeginInvoke(action, null); #endif actionInvoked = true; } } if (!actionInvoked) { action.Invoke(); } }
public static void MyGuiAsync(this Dispatcher dispatcher, Action action) { if(dispatcher.CheckAccess()) action(); else dispatcher.BeginInvoke(action, DispatcherPriority.Normal); }