/// <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)
        {
            _ = @this ?? throw new ArgumentNullException(nameof(@this));
            var tcs = TaskCompletionSourceExtensions.CreateAsyncTaskSource <object?>();

            @this.Post(state =>
            {
                try
                {
                    ((Action)state)();
                    tcs.TrySetResult(null);
                }
                catch (OperationCanceledException ex)
                {
                    tcs.TrySetCanceled(ex.CancellationToken);
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    tcs.TrySetException(ex);
                }
            }, action);
            return(tcs.Task);
        }
예제 #2
0
        Task <T> IAsyncWaitQueue <T> .Enqueue()
        {
            var tcs = TaskCompletionSourceExtensions.CreateAsyncTaskSource <T>();

            _queue.AddToBack(tcs);
            return(tcs.Task);
        }
예제 #3
0
 /// <summary>
 /// Creates an async-compatible manual-reset event.
 /// </summary>
 /// <param name="set">Whether the manual-reset event is initially set or unset.</param>
 public AsyncManualResetEvent(bool set)
 {
     _mutex = new object();
     _tcs   = TaskCompletionSourceExtensions.CreateAsyncTaskSource <object?>();
     if (set)
     {
         _tcs.TrySetResult(null);
     }
 }
예제 #4
0
 /// <summary>
 /// Resets the event. If the event is already reset, this method does nothing.
 /// </summary>
 public void Reset()
 {
     lock (_mutex)
     {
         if (_tcs.Task.IsCompleted)
         {
             _tcs = TaskCompletionSourceExtensions.CreateAsyncTaskSource <object?>();
         }
     }
 }
예제 #5
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);
        }
예제 #6
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);
        }
예제 #7
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);
        }
예제 #8
0
        /// <summary>
        /// Asynchronously executes an asynchronous 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 <Task <T> > action)
        {
            var tcs = TaskCompletionSourceExtensions.CreateAsyncTaskSource <T>();

            @this.Post(async state =>
            {
                try
                {
                    tcs.SetResult(await((Func <Task <T> >)state)().ConfigureAwait(false));
                }
                catch (OperationCanceledException ex)
                {
                    tcs.TrySetCanceled(ex.CancellationToken);
                }
                catch (Exception ex)
                {
                    tcs.TrySetException(ex);
                }
            }, action);
            return(tcs.Task);
        }
        /// <summary>
        /// Asynchronously executes an asynchronous 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 <Task <T> > action)
        {
            _ = @this ?? throw new ArgumentNullException(nameof(@this));
            var tcs = TaskCompletionSourceExtensions.CreateAsyncTaskSource <T>();

            @this.Post(async state =>
            {
                try
                {
                    tcs.SetResult(await((Func <Task <T> >)state)().ConfigureAwait(false));
                }
                catch (OperationCanceledException ex)
                {
                    tcs.TrySetCanceled(ex.CancellationToken);
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    tcs.TrySetException(ex);
                }
            }, action);
            return(tcs.Task);
        }