コード例 #1
0
 /// <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);
         }
     }
 }
コード例 #2
0
 /// <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);
         }
     }
 }
コード例 #3
0
        /// <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);
        }