This repository was archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 612
Expand file tree
/
Copy pathTaskUtils.cs
More file actions
149 lines (124 loc) · 4.64 KB
/
Copy pathTaskUtils.cs
File metadata and controls
149 lines (124 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) ServiceStack, Inc. All Rights Reserved.
// License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace ServiceStack
{
public static class TaskUtils
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task<T> FromResult<T>(T result) => Task.FromResult(result);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task<T> InTask<T>(this T result) => Task.FromResult(result);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task<T> InTask<T>(this Exception ex)
{
var taskSource = new TaskCompletionSource<T>();
taskSource.TrySetException(ex);
return taskSource.Task;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsSuccess(this Task task) => !task.IsFaulted && task.IsCompleted;
public static Task<To> Cast<From, To>(this Task<From> task) where To : From => task.Then(x => (To)x);
public static TaskScheduler SafeTaskScheduler()
{
//Unit test runner
if (SynchronizationContext.Current != null)
return TaskScheduler.FromCurrentSynchronizationContext();
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
return TaskScheduler.FromCurrentSynchronizationContext();
}
public static Task<To> Then<From, To>(this Task<From> task, Func<From, To> fn)
{
var tcs = new TaskCompletionSource<To>();
task.ContinueWith(t =>
{
if (t.IsFaulted)
tcs.TrySetException(t.Exception.InnerExceptions);
else if (t.IsCanceled)
tcs.TrySetCanceled();
else
tcs.TrySetResult(fn(t.Result));
}, TaskContinuationOptions.ExecuteSynchronously);
return tcs.Task;
}
public static Task Then(this Task task, Func<Task, Task> fn)
{
var tcs = new TaskCompletionSource<object>();
task.ContinueWith(t =>
{
if (t.IsFaulted)
tcs.TrySetException(t.Exception.InnerExceptions);
else if (t.IsCanceled)
tcs.TrySetCanceled();
else
tcs.TrySetResult(fn(t));
}, TaskContinuationOptions.ExecuteSynchronously);
return tcs.Task;
}
//http://stackoverflow.com/a/13904811/85785
public static Task EachAsync<T>(this IEnumerable<T> items, Func<T, int, Task> fn)
{
var tcs = new TaskCompletionSource<object>();
var enumerator = items.GetEnumerator();
var i = 0;
Action<Task> next = null;
next = t =>
{
if (t.IsFaulted)
tcs.TrySetException(t.Exception.InnerExceptions);
else if (t.IsCanceled)
tcs.TrySetCanceled();
else
StartNextIteration(tcs, fn, enumerator, ref i, next);
};
StartNextIteration(tcs, fn, enumerator, ref i, next);
tcs.Task.ContinueWith(_ => enumerator.Dispose(), TaskContinuationOptions.ExecuteSynchronously);
return tcs.Task;
}
static void StartNextIteration<T>(TaskCompletionSource<object> tcs,
Func<T, int, Task> fn,
IEnumerator<T> enumerator,
ref int i,
Action<Task> next)
{
bool moveNext;
try
{
moveNext = enumerator.MoveNext();
}
catch (Exception ex)
{
tcs.SetException(ex);
return;
}
if (!moveNext)
{
tcs.SetResult(null);
return;
}
Task iterationTask = null;
try
{
iterationTask = fn(enumerator.Current, i);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
i++;
iterationTask?.ContinueWith(next, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
}
public static void Sleep(int timeMs)
{
Thread.Sleep(timeMs);
}
public static void Sleep(TimeSpan time)
{
Thread.Sleep(time);
}
}
}