Beispiel #1
0
        /// <summary>
        /// Does something on threadpool, executes continuation on current sync context thread, executes onError if the async request fails.
        /// There does probably exist something like this in the .NET library, but I could not find it. //cocytus
        /// </summary>
        /// <typeparam name="T">Result to be passed from doMe to continueWith</typeparam>
        /// <param name="doMe">The stuff we want to do. Should return whatever continueWith expects.</param>
        /// <param name="continueWith">Do this on original sync context.</param>
        /// <param name="onError">Do this on original sync context if doMe barfs.</param>
        public static Task <T> DoAsync <T>(Func <T> doMe, Action <T> continueWith, Action <AsyncErrorEventArgs> onError)
        {
            AsyncLoader loader = new AsyncLoader();

            loader.LoadingError += (sender, e) => onError(e);
            return(loader.Load(doMe, continueWith));
        }
Beispiel #2
0
        /// <summary>
        /// Invokes <paramref name="loadContent"/> on the thread pool, then executes <paramref name="continueWith"/> on the current synchronisation context.
        /// </summary>
        /// <remarks>
        /// Note this method does not perform any cancellation of prior loads, nor does it support cancellation upon disposal.
        /// If you require those features, use an instance of <see cref="AsyncLoader"/> instead.
        /// </remarks>
        /// <typeparam name="T">Type of data returned by <paramref name="loadContent"/> and accepted by <paramref name="continueWith"/>.</typeparam>
        /// <param name="loadContent">A function to invoke on the thread pool that returns a value to be passed to <paramref name="continueWith"/>.</param>
        /// <param name="continueWith">An action to invoke on the original synchronisation context with the return value from <paramref name="loadContent"/>.</param>
        /// <param name="onError">
        /// An optional callback for notification of exceptions from <paramref name="loadContent"/>.
        /// Invoked on the original synchronisation context.
        /// Invoked once per exception, so may be called multiple times.
        /// Handlers must set <see cref="AsyncErrorEventArgs.Handled"/> to <c>true</c> to prevent any exceptions being re-thrown and faulting the async operation.
        /// </param>
        public static async Task <T> DoAsync <T>(Func <T> loadContent, Action <T> continueWith, Action <AsyncErrorEventArgs> onError = null)
        {
            using (var loader = new AsyncLoader())
            {
                if (onError != null)
                {
                    loader.LoadingError += (_, e) => onError(e);
                }

                return(await loader.LoadAsync(loadContent, continueWith).ConfigureAwait(false));
            }
        }
Beispiel #3
0
        public static Task DoAsync(Action doMe, Action continueWith)
        {
            AsyncLoader loader = new AsyncLoader();

            return(loader.Load(doMe, continueWith));
        }
Beispiel #4
0
        public static Task <T> DoAsync <T>(Func <T> doMe, Action <T> continueWith)
        {
            AsyncLoader loader = new AsyncLoader();

            return(loader.Load(doMe, continueWith));
        }
Beispiel #5
0
        public static void DoAsync <T>(Func <T> doMe, Action <T> continueWith)
        {
            AsyncLoader loader = new AsyncLoader();

            loader.Load(doMe, continueWith);
        }
Beispiel #6
0
        public static void DoAsync(Action doMe, Action continueWith)
        {
            AsyncLoader loader = new AsyncLoader();

            loader.Load(doMe, continueWith);
        }