Skip to content

Commit

Permalink
extend Task exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tibel committed Aug 29, 2014
1 parent d90b90c commit 047c292
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 26 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions build/Weakly.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
<metadata>
<id>Weakly</id>
<title>Weakly</title>
<version>2.1.4</version>
<version>2.2.0</version>
<authors>Thomas Ibel</authors>
<description>Weakly is a collection of some useful weak-reference types available as portable class library for net45+win8+wp8+wpa81.</description>
<language>en-US</language>
<licenseUrl>https://raw.github.com/tibel/Weakly/master/LICENSE</licenseUrl>
<projectUrl>https://github.com/tibel/Weakly</projectUrl>
<iconUrl>https://raw.github.com/tibel/Weakly/master/build/weakly_icon.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<releaseNotes>Optimizations</releaseNotes>
<releaseNotes>extend Task exception handling</releaseNotes>
<copyright>Copyright Thomas Ibel 2013-2014</copyright>
<tags>
Weakly WeakReference WeakAction WeakFunc WeakDelegate WeakCollection WeakValueDictionary WeakEventHandler WeakEventSource DynamicDelegate
Weakly WeakReference WeakAction WeakFunc WeakDelegate WeakCollection WeakValueDictionary WeakEventHandler WeakEventSource DynamicDelegate Async Task
</tags>
</metadata>

Expand Down
4 changes: 2 additions & 2 deletions src/GlobalAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
24 changes: 24 additions & 0 deletions src/Weakly/Collections/DictionaryHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;

namespace Weakly
{
/// <summary>
/// Common extensions to <see cref="IDictionary&lt;TKey,TValue&gt;"/>
/// </summary>
public static class DictionaryHelper
{
/// <summary>
/// Gets the value for a key. If the key does not exist, return default(TValue);
/// </summary>
/// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
/// <typeparam name="TValue">The type of the values in the dictionary.</typeparam>
/// <param name="dictionary">The dictionary to call this method on.</param>
/// <param name="key">The key to look up.</param>
/// <returns>The key value. default(TValue) if this key is not in the dictionary.</returns>
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
{
TValue result;
return dictionary.TryGetValue(key, out result) ? result : default(TValue);
}
}
}
14 changes: 0 additions & 14 deletions src/Weakly/Collections/EnumerableHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,5 @@ public static Task<TResult[]> SelectAsync<TSource, TResult>(this IEnumerable<TSo
{
return Task.WhenAll(source.Select(asyncSelector));
}

/// <summary>
/// Gets the value for a key. If the key does not exist, return default(TValue);
/// </summary>
/// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
/// <typeparam name="TValue">The type of the values in the dictionary.</typeparam>
/// <param name="dictionary">The dictionary to call this method on.</param>
/// <param name="key">The key to look up.</param>
/// <returns>The key value. default(TValue) if this key is not in the dictionary.</returns>
public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
{
TValue result;
return dictionary.TryGetValue(key, out result) ? result : default(TValue);
}
}
}
File renamed without changes.
4 changes: 2 additions & 2 deletions src/Weakly/Expressions/ExpressionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Weakly
{
/// <summary>
/// Extension for <see cref="Expression"/>.
/// Common extensions for <see cref="Expression"/>.
/// </summary>
public static class ExpressionHelper
{
Expand All @@ -13,7 +13,7 @@ public static class ExpressionHelper
/// </summary>
/// <param name="expression">The expression to convert.</param>
/// <returns>The member info.</returns>
public static MemberInfo GetMemberInfo(this Expression expression)
public static MemberInfo GetMemberInfo(Expression expression)
{
var lambda = (LambdaExpression)expression;

Expand Down
33 changes: 33 additions & 0 deletions src/Weakly/Tasks/FaultedTaskEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Threading.Tasks;

namespace Weakly
{
/// <summary>
/// Provides data for the event that is raised when a faulted <see cref="Task"/> is observed.
/// </summary>
public sealed class FaultedTaskEventArgs : EventArgs
{
private readonly Task _task;

/// <summary>
/// Initializes a new instance of the <see cref="FaultedTaskEventArgs"/> class with the faulted task.
/// </summary>
/// <param name="task">The Task that has faulted.</param>
public FaultedTaskEventArgs(Task task)
{
if (task == null)
throw new ArgumentNullException("task");

_task = task;
}

/// <summary>
/// The faulted task.
/// </summary>
public Task Task
{
get { return _task; }
}
}
}
68 changes: 68 additions & 0 deletions src/Weakly/Tasks/TaskHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Weakly
/// </summary>
public static class TaskHelper
{
#region Common Tasks

private readonly static Task CompletedTask = Task.FromResult<object>(null);

/// <summary>
Expand Down Expand Up @@ -73,6 +75,10 @@ public static Task Faulted(Exception ex)
return Faulted<object>(ex);
}

#endregion

#region Exception handling

/// <summary>
/// Suppresses default exception handling of a Task that would otherwise reraise the exception on the finalizer thread.
/// </summary>
Expand All @@ -97,5 +103,67 @@ public static Task<T> IgnoreExceptions<T>(this Task<T> task)
{
return (Task<T>)((Task)task).IgnoreExceptions();
}

/// <summary>
/// Fails immediately when an exception is encountered.
/// </summary>
/// <param name="task">The Task to be monitored.</param>
/// <returns>The original Task.</returns>
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;
}

/// <summary
/// >Fails immediately when an exception is encountered.
/// </summary>
/// <param name="task">The Task to be monitored.</param>
/// <returns>The original Task.</returns>
public static Task<T> FailFastOnException<T>(this Task<T> task)
{
return (Task<T>)((Task)task).FailFastOnException();
}

/// <summary>
/// Occurs when a faulted <see cref="Task"/> is observed.
/// </summary>
public static event EventHandler<FaultedTaskEventArgs> TaskFaulted;

private static void OnTaskFaulted(Task task)
{
var handler = TaskFaulted;
if (handler != null)
handler(null, new FaultedTaskEventArgs(task));
}

/// <summary>
/// Triggers the <see cref="TaskFaulted"/> event immediately when an exception is encountered.
/// </summary>
/// <param name="task">The Task to be monitored.</param>
/// <returns>The original Task.</returns>
public static Task ObserveException(this Task task)
{
task.ContinueWith(OnTaskFaulted,
CancellationToken.None,
TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted,
TaskScheduler.Default);
return task;
}

/// <summary>
/// Triggers the <see cref="TaskFaulted"/> event immediately when an exception is encountered.
/// </summary>
/// <param name="task">The Task to be monitored.</param>
/// <returns>The original Task.</returns>
public static Task<T> ObserveException<T>(this Task<T> task)
{
return (Task<T>)((Task)task).ObserveException();
}

#endregion
}
}
4 changes: 3 additions & 1 deletion src/Weakly/Weakly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@
<Compile Include="..\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Collections\DictionaryHelper.cs" />
<Compile Include="Collections\EnumerableHelper.cs" />
<Compile Include="Collections\SimpleCache.cs" />
<Compile Include="Collections\WeakCollection.cs" />
<Compile Include="Collections\WeakValueDictionary.cs" />
<Compile Include="Delegates\ActionDisposable.cs" />
<Compile Include="Delegates\DisposableAction.cs" />
<Compile Include="Reflection\ReflectionHelper.cs" />
<Compile Include="Reflection\DynamicDelegate.cs" />
<Compile Include="Delegates\OpenAction.cs" />
Expand All @@ -69,6 +70,7 @@
<Compile Include="Delegates\WeakAction.cs" />
<Compile Include="Events\WeakEventSource.cs" />
<Compile Include="Delegates\WeakFunc.cs" />
<Compile Include="Tasks\FaultedTaskEventArgs.cs" />
<Compile Include="Tasks\TaskHelper.cs" />
</ItemGroup>
<ItemGroup />
Expand Down

0 comments on commit 047c292

Please sign in to comment.