Esempio n. 1
0
        /// <summary>
        /// Asynchronously executes a delegate on this synchronization context and returns its result.
        /// </summary>
        /// <typeparam name="T">The type of the result.</typeparam>
        /// <param name="this">The synchronization context.</param>
        /// <param name="action">The delegate to execute.</param>
        public static Task <T> PostAsync <T>(this SynchronizationContext @this, Func <T> action)
        {
            var tcs = TaskCompletionSourceExtensions.CreateAsyncTaskSource <T>();

            @this.Post(state =>
            {
                try
                {
                    tcs.SetResult(((Func <T>)state)());
                }
                catch (Exception ex)
                {
                    tcs.TrySetException(ex);
                }
            }, action);
            return(tcs.Task);
        }
Esempio n. 2
0
        /// <summary>
        /// Asynchronously executes an asynchronous delegate on this synchronization context.
        /// </summary>
        /// <param name="this">The synchronization context.</param>
        /// <param name="action">The delegate to execute.</param>
        public static Task PostAsync(this SynchronizationContext @this, Func <Task> action)
        {
            var tcs = TaskCompletionSourceExtensions.CreateAsyncTaskSource <object>();

            @this.Post(async state =>
            {
                try
                {
                    await((Func <Task>)state)().ConfigureAwait(false);
                    tcs.TrySetResult(null);
                }
                catch (Exception ex)
                {
                    tcs.TrySetException(ex);
                }
            }, action);
            return(tcs.Task);
        }
Esempio n. 3
0
        /// <summary>
        /// Asynchronously executes a delegate on this synchronization context.
        /// </summary>
        /// <param name="this">The synchronization context.</param>
        /// <param name="action">The delegate to execute.</param>
        public static Task PostAsync(this SynchronizationContext @this, Action action)
        {
            var tcs = TaskCompletionSourceExtensions.CreateAsyncTaskSource <object>();

            @this.Post(state =>
            {
                try
                {
                    ((Action)state)();
                    tcs.TrySetResult(null);
                }
                catch (Exception ex)
                {
                    tcs.TrySetException(ex);
                }
            }, action);
            return(tcs.Task);
        }