/// <summary> /// Send a delegate to a <see cref="SynchronizationContext"/> to invoke immediately. /// </summary> /// <param name="syncContext"></param> /// <param name="callback"></param> /// <param name="args"></param> public static void Invoke(this SynchronizationContext syncContext, Delegate callback, params object[] args) { if (callback != null) { if (syncContext.CheckAccess()) { callback.DynamicInvoke(args); } else { syncContext.Send(o => { callback.DynamicInvoke(args); }, args); } } }
/// <summary> /// Send an action to a <see cref="SynchronizationContext"/> to invoke immediately. /// </summary> /// <param name="syncContext"></param> /// <param name="callback"></param> public static void Invoke(this SynchronizationContext syncContext, Action callback) { if (callback != null) { if (syncContext.CheckAccess()) { callback(); } else { syncContext.Send(o => { callback(); }, null); } } }
/// <summary> /// Send a function with a return type to a <see cref="SynchronizationContext"/> to /// invoke immediately and return the result. /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="syncContext"></param> /// <param name="callback"></param> /// <returns></returns> public static TResult Invoke <TResult>(this SynchronizationContext syncContext, Func <TResult> callback) { TResult retVal = default(TResult); if (callback != null) { if (syncContext.CheckAccess()) { callback(); } else { syncContext.Send(o => { retVal = callback(); }, retVal); } } return(retVal); }