Beispiel #1
0
        private static async Task<T> RunWithResultAsync<T>(this Dispatcher dispatcher, Func<T> function, DispatcherPriority priority)
        {
            var result = default(T);

#if UWP
            await dispatcher.RunAsync(priority.ToCoreDispatcherPriority(), () =>
            {
                result = function();
            });
#else
            var tcs = new TaskCompletionSource<T>();

            var dispatcherOperation = dispatcher.BeginInvoke(new Action(() =>
            {
                try
                {
                    result = function();
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            }), priority, null);

            dispatcherOperation.Completed += (sender, e) => SetResult(tcs, result);
            dispatcherOperation.Aborted += (sender, e) => SetCanceled(tcs);

            await tcs.Task;
#endif

            return result;
        }
Beispiel #2
0
        private static Task RunAsync(this Dispatcher dispatcher, Action action, DispatcherPriority priority)
        {
#if UWP
            var task = dispatcher.RunAsync(priority.ToCoreDispatcherPriority(), () =>
            {
                action();
            });

            return task.AsTask();
#else
            var tcs = new TaskCompletionSource<bool>();

            var dispatcherOperation = dispatcher.BeginInvoke(new Action(() =>
            {
                try
                {
                    action();

                    SetResult(tcs, true);
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            }), priority, null);

            dispatcherOperation.Aborted += (sender, e) => SetCanceled(tcs);

            return tcs.Task;
#endif
        }
Beispiel #3
0
        private static async Task<T> RunWithResultAsync<T>(this Dispatcher dispatcher, Func<CancellationToken, Task<T>> functionAsync,
            CancellationToken cancellationToken, DispatcherPriority priority)
        {
            var tcs = new TaskCompletionSource<T>();
#if UWP
            await dispatcher.RunAsync(priority.ToCoreDispatcherPriority(), async () =>

#else
            var dispatcherOperation = dispatcher.BeginInvoke(new Action(async () =>
#endif
            {
                try
                {
                    var task = functionAsync(cancellationToken);

                    await task;

                    if (task.IsFaulted)
                    {
                        tcs.TrySetException(task.Exception ?? new Exception("Unknown error"));
                        return;
                    }

                    if (task.IsCanceled)
                    {
                        SetCanceled(tcs);
                        return;
                    }

                    SetResult(tcs, task.Result);
                }
                catch (Exception ex)
                {
                    // NOTE: in theory, it could have been already set before
                    tcs.TrySetException(ex);
                }
#if !UWP
            }), priority, null);

            // IMPORTANT: don't handle 'dispatcherOperation.Completed' event.
            // We should only signal to awaiter when the operation is really done

            dispatcherOperation.Aborted += (sender, e) => SetCanceled(tcs);
#else
            });