From 047c292f67f8fbd60cde00c8841d28d520caf47a Mon Sep 17 00:00:00 2001 From: Thomas Ibel Date: Fri, 29 Aug 2014 19:02:12 +0200 Subject: [PATCH] extend Task exception handling --- README.md | 21 ++++-- build/Weakly.nuspec | 6 +- src/GlobalAssemblyInfo.cs | 4 +- src/Weakly/Collections/DictionaryHelper.cs | 24 +++++++ src/Weakly/Collections/EnumerableHelper.cs | 14 ---- ...ctionDisposable.cs => DisposableAction.cs} | 0 src/Weakly/Expressions/ExpressionHelper.cs | 4 +- src/Weakly/Tasks/FaultedTaskEventArgs.cs | 33 +++++++++ src/Weakly/Tasks/TaskHelper.cs | 68 +++++++++++++++++++ src/Weakly/Weakly.csproj | 4 +- 10 files changed, 152 insertions(+), 26 deletions(-) create mode 100644 src/Weakly/Collections/DictionaryHelper.cs rename src/Weakly/Delegates/{ActionDisposable.cs => DisposableAction.cs} (100%) create mode 100644 src/Weakly/Tasks/FaultedTaskEventArgs.cs diff --git a/README.md b/README.md index 84a943a..0309b97 100644 --- a/README.md +++ b/README.md @@ -17,16 +17,29 @@ Weakly is available through NuGet: ### Collections * WeakCollection<T> * WeakValueDictionary<TKey, TValue> +* Extensions for IEnumerable<T> and IDictionary<TKey,TValue> + +### Delegates +* WeakAction to WeakAction<T1, T2, T3, T4, T5> +* WeakFunc<TResult> to WeakFunc<T1, T2, T3, T4, T5, TResult> +* DisposableAction ### Events * WeakEventHandler<TEventArgs> * WeakEventSource<TEventArgs> -### Delegates -* WeakAction to WeakAction<T1, T2, T3, T4, T5> -* WeakFunc<TResult> to WeakFunc<T1, T2, T3, T4, T5, TResult> +### Expressions +* Extensions for Expression + +### IO +* MemoryTributary ### Reflection * DynamicDelegate (compiled version of MethodInfo.Invoke) -* DynamicProperty (compiled version of PropertyInfo.SetValue and GetValue) * DynamicEvent (compiled version of EventInfo.AddEventHandler and RemoveEventHandler) +* DynamicProperty (compiled version of PropertyInfo.SetValue and GetValue) +* some Helpers + +### Tasks +* Common Tasks +* Exception handling diff --git a/build/Weakly.nuspec b/build/Weakly.nuspec index 29062ee..505c73f 100644 --- a/build/Weakly.nuspec +++ b/build/Weakly.nuspec @@ -3,7 +3,7 @@ Weakly Weakly - 2.1.4 + 2.2.0 Thomas Ibel Weakly is a collection of some useful weak-reference types available as portable class library for net45+win8+wp8+wpa81. en-US @@ -11,10 +11,10 @@ https://github.com/tibel/Weakly https://raw.github.com/tibel/Weakly/master/build/weakly_icon.png false - Optimizations + extend Task exception handling Copyright Thomas Ibel 2013-2014 - Weakly WeakReference WeakAction WeakFunc WeakDelegate WeakCollection WeakValueDictionary WeakEventHandler WeakEventSource DynamicDelegate + Weakly WeakReference WeakAction WeakFunc WeakDelegate WeakCollection WeakValueDictionary WeakEventHandler WeakEventSource DynamicDelegate Async Task diff --git a/src/GlobalAssemblyInfo.cs b/src/GlobalAssemblyInfo.cs index 9bc82cb..194bcfb 100644 --- a/src/GlobalAssemblyInfo.cs +++ b/src/GlobalAssemblyInfo.cs @@ -12,5 +12,5 @@ [assembly: CLSCompliant(true)] -[assembly: AssemblyVersion("2.1.0.0")] -[assembly: AssemblyFileVersion("2.1.4.0")] +[assembly: AssemblyVersion("2.2.0.0")] +[assembly: AssemblyFileVersion("2.2.0.0")] diff --git a/src/Weakly/Collections/DictionaryHelper.cs b/src/Weakly/Collections/DictionaryHelper.cs new file mode 100644 index 0000000..fb01691 --- /dev/null +++ b/src/Weakly/Collections/DictionaryHelper.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; + +namespace Weakly +{ + /// + /// Common extensions to + /// + public static class DictionaryHelper + { + /// + /// Gets the value for a key. If the key does not exist, return default(TValue); + /// + /// The type of the keys in the dictionary. + /// The type of the values in the dictionary. + /// The dictionary to call this method on. + /// The key to look up. + /// The key value. default(TValue) if this key is not in the dictionary. + public static TValue GetValueOrDefault(this IDictionary dictionary, TKey key) + { + TValue result; + return dictionary.TryGetValue(key, out result) ? result : default(TValue); + } + } +} diff --git a/src/Weakly/Collections/EnumerableHelper.cs b/src/Weakly/Collections/EnumerableHelper.cs index 64302b4..9209f63 100644 --- a/src/Weakly/Collections/EnumerableHelper.cs +++ b/src/Weakly/Collections/EnumerableHelper.cs @@ -48,19 +48,5 @@ public static Task SelectAsync(this IEnumerable - /// Gets the value for a key. If the key does not exist, return default(TValue); - /// - /// The type of the keys in the dictionary. - /// The type of the values in the dictionary. - /// The dictionary to call this method on. - /// The key to look up. - /// The key value. default(TValue) if this key is not in the dictionary. - public static TValue GetValueOrDefault(this IDictionary dictionary, TKey key) - { - TValue result; - return dictionary.TryGetValue(key, out result) ? result : default(TValue); - } } } diff --git a/src/Weakly/Delegates/ActionDisposable.cs b/src/Weakly/Delegates/DisposableAction.cs similarity index 100% rename from src/Weakly/Delegates/ActionDisposable.cs rename to src/Weakly/Delegates/DisposableAction.cs diff --git a/src/Weakly/Expressions/ExpressionHelper.cs b/src/Weakly/Expressions/ExpressionHelper.cs index f96a8a4..368d908 100644 --- a/src/Weakly/Expressions/ExpressionHelper.cs +++ b/src/Weakly/Expressions/ExpressionHelper.cs @@ -4,7 +4,7 @@ namespace Weakly { /// - /// Extension for . + /// Common extensions for . /// public static class ExpressionHelper { @@ -13,7 +13,7 @@ public static class ExpressionHelper /// /// The expression to convert. /// The member info. - public static MemberInfo GetMemberInfo(this Expression expression) + public static MemberInfo GetMemberInfo(Expression expression) { var lambda = (LambdaExpression)expression; diff --git a/src/Weakly/Tasks/FaultedTaskEventArgs.cs b/src/Weakly/Tasks/FaultedTaskEventArgs.cs new file mode 100644 index 0000000..a304816 --- /dev/null +++ b/src/Weakly/Tasks/FaultedTaskEventArgs.cs @@ -0,0 +1,33 @@ +using System; +using System.Threading.Tasks; + +namespace Weakly +{ + /// + /// Provides data for the event that is raised when a faulted is observed. + /// + public sealed class FaultedTaskEventArgs : EventArgs + { + private readonly Task _task; + + /// + /// Initializes a new instance of the class with the faulted task. + /// + /// The Task that has faulted. + public FaultedTaskEventArgs(Task task) + { + if (task == null) + throw new ArgumentNullException("task"); + + _task = task; + } + + /// + /// The faulted task. + /// + public Task Task + { + get { return _task; } + } + } +} diff --git a/src/Weakly/Tasks/TaskHelper.cs b/src/Weakly/Tasks/TaskHelper.cs index 4ceffbf..8247af6 100644 --- a/src/Weakly/Tasks/TaskHelper.cs +++ b/src/Weakly/Tasks/TaskHelper.cs @@ -9,6 +9,8 @@ namespace Weakly /// public static class TaskHelper { + #region Common Tasks + private readonly static Task CompletedTask = Task.FromResult(null); /// @@ -73,6 +75,10 @@ public static Task Faulted(Exception ex) return Faulted(ex); } + #endregion + + #region Exception handling + /// /// Suppresses default exception handling of a Task that would otherwise reraise the exception on the finalizer thread. /// @@ -97,5 +103,67 @@ public static Task IgnoreExceptions(this Task task) { return (Task)((Task)task).IgnoreExceptions(); } + + /// + /// Fails immediately when an exception is encountered. + /// + /// The Task to be monitored. + /// The original Task. + public static Task FailFastOnException(this Task task) + { + task.ContinueWith(t => Environment.FailFast("A task faulted.", t.Exception), + CancellationToken.None, + TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted, + TaskScheduler.Default); + return task; + } + + /// Fails immediately when an exception is encountered. + /// + /// The Task to be monitored. + /// The original Task. + public static Task FailFastOnException(this Task task) + { + return (Task)((Task)task).FailFastOnException(); + } + + /// + /// Occurs when a faulted is observed. + /// + public static event EventHandler TaskFaulted; + + private static void OnTaskFaulted(Task task) + { + var handler = TaskFaulted; + if (handler != null) + handler(null, new FaultedTaskEventArgs(task)); + } + + /// + /// Triggers the event immediately when an exception is encountered. + /// + /// The Task to be monitored. + /// The original Task. + public static Task ObserveException(this Task task) + { + task.ContinueWith(OnTaskFaulted, + CancellationToken.None, + TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted, + TaskScheduler.Default); + return task; + } + + /// + /// Triggers the event immediately when an exception is encountered. + /// + /// The Task to be monitored. + /// The original Task. + public static Task ObserveException(this Task task) + { + return (Task)((Task)task).ObserveException(); + } + + #endregion } } diff --git a/src/Weakly/Weakly.csproj b/src/Weakly/Weakly.csproj index 3ab2b88..ef1ae32 100644 --- a/src/Weakly/Weakly.csproj +++ b/src/Weakly/Weakly.csproj @@ -50,11 +50,12 @@ Properties\GlobalAssemblyInfo.cs + - + @@ -69,6 +70,7 @@ +