annotate Implab/TaskHelpers.cs @ 253:34df34841225 v3 v3.0.1-beta

Implab.Diagnostics drafts
author cin
date Mon, 12 Feb 2018 17:03:37 +0300
parents 7c7e9ad6fe4a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
251
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
1 using System;
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
2 using System.Threading.Tasks;
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
3
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
4 namespace Implab {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
5 public static class TaskHelpers {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
6
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
7 public static async Task Then(this Task that, Action fulfilled, Action<Exception> rejected) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
8 Safe.ArgumentNotNull(that, nameof(that));
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
9 if (rejected != null) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
10 try {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
11 await that;
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
12 } catch (Exception e) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
13 rejected(e);
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
14 return;
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
15 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
16 } else {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
17 await that;
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
18 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
19
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
20 if (fulfilled != null)
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
21 fulfilled();
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
22 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
23
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
24 public static async Task Then(this Task that, Action fulfilled) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
25 Safe.ArgumentNotNull(that, nameof(that));
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
26 await that;
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
27 if (fulfilled != null)
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
28 fulfilled();
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
29 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
30
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
31 public static async Task Then(this Task that, Func<Task> fulfilled) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
32 Safe.ArgumentNotNull(that, nameof(that));
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
33 await that;
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
34 if (fulfilled != null)
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
35 await fulfilled();
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
36 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
37
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
38 public static async Task Finally(this Task that, Action handler) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
39 Safe.ArgumentNotNull(that, nameof(that));
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
40 try {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
41 await that;
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
42 } finally {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
43 if (handler != null)
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
44 handler();
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
45 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
46 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
47
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
48 public static async void Then(this Task that, IResolvable next) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
49 try {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
50 await that;
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
51 } catch (Exception e) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
52 next.Reject(e);
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
53 return;
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
54 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
55 next.Resolve();
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
56 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
57
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
58 public static async void Then<T>(this Task<T> that, IResolvable<T> next) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
59 T result;
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
60 try {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
61 result = await that;
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
62 } catch (Exception e) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
63 next.Reject(e);
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
64 return;
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
65 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
66 next.Resolve(result);
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
67 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
68
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
69 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents:
diff changeset
70 }