mscorlib(4.0.0.0) API with additions
AsyncHelper.cs
2 
3 namespace System.Xml
4 {
5  internal static class AsyncHelper
6  {
7  public static readonly Task DoneTask = Task.FromResult(result: true);
8 
9  public static readonly Task<bool> DoneTaskTrue = Task.FromResult(result: true);
10 
11  public static readonly Task<bool> DoneTaskFalse = Task.FromResult(result: false);
12 
13  public static readonly Task<int> DoneTaskZero = Task.FromResult(0);
14 
15  public static bool IsSuccess(this Task task)
16  {
17  if (task.IsCompleted)
18  {
19  return task.Exception == null;
20  }
21  return false;
22  }
23 
24  public static Task CallVoidFuncWhenFinish(this Task task, Action func)
25  {
26  if (task.IsSuccess())
27  {
28  func();
29  return DoneTask;
30  }
31  return task._CallVoidFuncWhenFinish(func);
32  }
33 
34  private static async Task _CallVoidFuncWhenFinish(this Task task, Action func)
35  {
36  await task.ConfigureAwait(continueOnCapturedContext: false);
37  func();
38  }
39 
40  public static Task<bool> ReturnTaskBoolWhenFinish(this Task task, bool ret)
41  {
42  if (task.IsSuccess())
43  {
44  if (ret)
45  {
46  return DoneTaskTrue;
47  }
48  return DoneTaskFalse;
49  }
50  return task._ReturnTaskBoolWhenFinish(ret);
51  }
52 
53  public static async Task<bool> _ReturnTaskBoolWhenFinish(this Task task, bool ret)
54  {
55  await task.ConfigureAwait(continueOnCapturedContext: false);
56  return ret;
57  }
58 
59  public static Task CallTaskFuncWhenFinish(this Task task, Func<Task> func)
60  {
61  if (task.IsSuccess())
62  {
63  return func();
64  }
65  return _CallTaskFuncWhenFinish(task, func);
66  }
67 
68  private static async Task _CallTaskFuncWhenFinish(Task task, Func<Task> func)
69  {
70  await task.ConfigureAwait(continueOnCapturedContext: false);
71  await func().ConfigureAwait(continueOnCapturedContext: false);
72  }
73 
74  public static Task<bool> CallBoolTaskFuncWhenFinish(this Task task, Func<Task<bool>> func)
75  {
76  if (task.IsSuccess())
77  {
78  return func();
79  }
80  return task._CallBoolTaskFuncWhenFinish(func);
81  }
82 
83  private static async Task<bool> _CallBoolTaskFuncWhenFinish(this Task task, Func<Task<bool>> func)
84  {
85  await task.ConfigureAwait(continueOnCapturedContext: false);
86  return await func().ConfigureAwait(continueOnCapturedContext: false);
87  }
88 
89  public static Task<bool> ContinueBoolTaskFuncWhenFalse(this Task<bool> task, Func<Task<bool>> func)
90  {
91  if (task.IsSuccess())
92  {
93  if (task.Result)
94  {
95  return DoneTaskTrue;
96  }
97  return func();
98  }
99  return _ContinueBoolTaskFuncWhenFalse(task, func);
100  }
101 
102  private static async Task<bool> _ContinueBoolTaskFuncWhenFalse(Task<bool> task, Func<Task<bool>> func)
103  {
104  if (await task.ConfigureAwait(continueOnCapturedContext: false))
105  {
106  return true;
107  }
108  return await func().ConfigureAwait(continueOnCapturedContext: false);
109  }
110  }
111 }
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Configures an awaiter used to await this T:System.Threading.Tasks.Task`1.
Definition: Task.cs:393
TResult Result
Gets the result value of this T:System.Threading.Tasks.Task`1.
Definition: Task.cs:57
Definition: __Canon.cs:3
delegate void Action()
Encapsulates a method that has no parameters and does not return a value.
bool IsCompleted
Gets whether this T:System.Threading.Tasks.Task has completed.
Definition: Task.cs:1370
AggregateException Exception
Gets the T:System.AggregateException that caused the T:System.Threading.Tasks.Task to end prematurely...
Definition: Task.cs:1277
Represents an asynchronous operation that can return a value.
Definition: Task.cs:18