by
16
8
550k
5
Top 1% !
Popular
Pearl of Wisdom
Nice
Easy-to-find
Specified
Refined
MultiPlatform
Popularity: 1160th place
ClassNameExtensions
InputName0task
InputName1onError
InputType0Task<T>
InputType1Func<TError,T>
Languagecs
LicenseMIT_X11
MethodNameCatch
OutputTypeTask<T>

Async Queue in C#

Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/458089/Async-Queue-in-C-?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
using System; using System.Net; using System.Threading; using System.Threading.Tasks; using System.Collections.Generic; using System.Linq; using System.Web; public class ParalyzKue { readonly object _locker = new object(); Task _tail = Task.FromResult (true); public Task Enqueue (Action action) { lock (_locker) return _tail = _tail.ContinueWith (ant => action()); } public Task<TResult> Enqueue<TResult> (Func<TResult> func) { lock (_locker) return (Task<TResult>) (_tail = _tail.ContinueWith (ant => func())); } public Task Enqueue (Func<Task> action) { lock (_locker) return _tail = _tail.Catch().Then (action); } public Task<TResult> Enqueue<TResult> (Func<Task<TResult>> func) { lock (_locker) return (Task<TResult>) (_tail = _tail.Catch().Then (func)); } } public static class Extensions { public static Task<T> Catch<T,TError> (this Task<T> task, Func<TError,T> onError) where TError : Exception { var tcs = new TaskCompletionSource<T> (); task.ContinueWith (ant => { if (task.IsFaulted && task.Exception.InnerException is TError) { tcs.SetResult (onError ((TError) task.Exception.InnerException)); } else if (ant.IsCanceled) { tcs.SetCanceled (); } else if (task.IsFaulted) { tcs.SetException (ant.Exception.InnerException); } else { tcs.SetResult (ant.Result); } }); return tcs.Task; } public static Task<T> Catch<T,TError> (this Task<T> task, T onError) where TError : Exception { return task.Catch<T,TError> (ex => onError); } public static Task<T> Catch<T,TError> (this Task task, T onError) where TError : Exception { return task.ToTaskOfT<T>().Catch<T,TError> (onError); } public static Task Catch (this Task task) { return task.Catch<object,Exception> (null); } public static Task<T> Catch<T> (this Task<T> task, T valueIfError = default(T)) { return task.Catch<T,Exception> (valueIfError); } public async static Task<T> ToTaskOfT<T> (this Task t) { await t; return default(T); } public static async Task Then(this Task task, Func<Task> continuation) { await task; await continuation(); } public static async Task<TNewResult> Then<TNewResult>(this Task task, Func<Task<TNewResult>> continuation) { await task; return await continuation(); } public static async Task Then<TResult>(this Task<TResult> task, Func<TResult,Task> continuation) { await continuation(await task); } public static async Task<TNewResult> Then<TResult, TNewResult>(this Task<TResult> task, Func<TResult, Task<TNewResult>> continuation) { return await continuation(await task); } } /* public void doKue() { var pcQ = new ParaKue(); pcQ.Enqueue (() => AsyncMethod1 ("http://www.rotten.com/")).Dump ("Task 1"); pcQ.Enqueue (() => AsyncMethod1 ("https://youtube.com/")).Dump ("Task 2"); pcQ.Enqueue (() => AsyncMethod1 ("https://yahoo.com/search?q=the cube")).Dump ("Task 3"); pcQ.Enqueue (() => AsyncMethod1 ("http://www.rotten.com/")).Dump ("Task 4"); pcQ.Enqueue (() => AsyncMethod1 ("https://youtube.com/")).Dump ("Task 5"); pcQ.Enqueue (() => AsyncMethod1 ("https://www.google.com/search?q=web scrapers")).Dump ("Task 6"); pcQ.Enqueue (() => AsyncMethod1 ("http://www.rotten.com/")).Dump ("Task 7"); pcQ.Enqueue (() => AsyncMethod1 ("https://youtube.com/")).Dump ("Task 8"); pcQ.Enqueue (() => AsyncMethod1 ("https://youtube.com/search?q=aliens gods")).Dump ("Task 9"); pcQ.Enqueue (() => AsyncMethod ("http://www.rotten.com/")).Dump ("Task 10"); pcQ.Enqueue (() => AsyncMethod ("https://youtube.com/")).Dump ("Task 11"); pcQ.Enqueue (() => AsyncMethod ("https://www.google.com/search?q=javascript")).Dump ("Task 12"); pcQ.Enqueue (() => AsyncMethod ("http://www.rotten.com/")).Dump ("Task 13"); pcQ.Enqueue (() => AsyncMethod ("https://youtube.com/search?q=aliens")).Dump ("Task 14"); pcQ.Enqueue (() => AsyncMethod ("https://www.yahoo.com/")).Dump ("Task 15"); pcQ.Enqueue (() => AsyncMethod ("https://youtube.com/")).Dump ("Task 16"); pcQ.Enqueue (() => AsyncMethod ("https://www.google.com/search?q=aliens disclosure")).Dump ("Task 17"); } string SyncMethod (string uri) { var x = new System.Net.WebClient(); var y = x.DownloadString(uri); return y; } async Task<string> AsyncMethod (string uri) { return await Task.Run(() => new System.Net.WebClient().DownloadStringTaskAsync(uri).Dump()); } async Task<string> AsyncMethod1 (string uri) { var x = await new System.Net.WebClient().DownloadStringTaskAsync(uri); return x; } */
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus