Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit b17866a

Browse files
committed
Add Success / Error convenience txt methods for empty Task
1 parent 7ae72b7 commit b17866a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/ServiceStack.Text/TaskExtensions.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ public static Task<T> Success<T>(this Task<T> task, Action<T> fn,
2222
return task;
2323
}
2424

25+
public static Task Success(this Task task, Action fn,
26+
bool onUiThread = true,
27+
TaskContinuationOptions taskOptions = TaskContinuationOptions.NotOnFaulted | TaskContinuationOptions.OnlyOnRanToCompletion)
28+
{
29+
if (onUiThread)
30+
{
31+
var source = new CancellationToken();
32+
task.ContinueWith(t => fn(), source, taskOptions, TaskScheduler.FromCurrentSynchronizationContext());
33+
}
34+
else
35+
{
36+
task.ContinueWith(t => fn(), taskOptions);
37+
}
38+
return task;
39+
}
40+
2541
public static Task<T> Error<T>(this Task<T> task, Action<Exception> fn,
2642
bool onUiThread = true,
2743
TaskContinuationOptions taskOptions = TaskContinuationOptions.OnlyOnFaulted)
@@ -38,11 +54,32 @@ public static Task<T> Error<T>(this Task<T> task, Action<Exception> fn,
3854
return task;
3955
}
4056

57+
public static Task Error(this Task task, Action<Exception> fn,
58+
bool onUiThread = true,
59+
TaskContinuationOptions taskOptions = TaskContinuationOptions.OnlyOnFaulted)
60+
{
61+
if (onUiThread)
62+
{
63+
var source = new CancellationToken();
64+
task.ContinueWith(t => fn(t.UnwrapIfSingleException()), source, taskOptions, TaskScheduler.FromCurrentSynchronizationContext());
65+
}
66+
else
67+
{
68+
task.ContinueWith(t => fn(t.UnwrapIfSingleException()), taskOptions);
69+
}
70+
return task;
71+
}
72+
4173
public static Exception UnwrapIfSingleException<T>(this Task<T> task)
4274
{
4375
return task.Exception.UnwrapIfSingleException();
4476
}
4577

78+
public static Exception UnwrapIfSingleException(this Task task)
79+
{
80+
return task.Exception.UnwrapIfSingleException();
81+
}
82+
4683
public static Exception UnwrapIfSingleException(this Exception ex)
4784
{
4885
var aex = ex as AggregateException;

0 commit comments

Comments
 (0)