-
Notifications
You must be signed in to change notification settings - Fork 2k
/
AsyncResultHandle.cs
84 lines (73 loc) · 2.47 KB
/
AsyncResultHandle.cs
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
using System;
using System.Threading.Tasks;
namespace Orleans.TestingHost.Utils
{
/// <summary>
/// This class is for internal testing use only.
/// </summary>
public class AsyncResultHandle
{
bool done = false;
bool continueFlag = false;
/// <summary> Reset the current result handle </summary>
public virtual void Reset()
{
Exception = null;
Result = null;
done = false;
continueFlag = false;
}
/// <summary> Get or set the Done flag </summary>
public bool Done
{
get { return done; }
set { done = value; }
}
/// <summary> Get or set the Continue flag </summary>
public bool Continue
{
get { return continueFlag; }
set { continueFlag = value; }
}
/// <summary> Get or set the exception of the result handle </summary>
public Exception Exception { get; set; }
/// <summary> Get or set the value of the result handle </summary>
public object Result { get; set; }
/// <summary>
/// </summary>
/// <param name="timeout"></param>
/// <returns>Returns <c>true</c> if operation completes before timeout</returns>
public Task<bool> WaitForFinished(TimeSpan timeout)
{
return WaitFor(timeout, () => done);
}
/// <summary>
/// </summary>
/// <param name="timeout"></param>
/// <returns>Returns <c>true</c> if operation completes before timeout</returns>
public Task<bool> WaitForContinue(TimeSpan timeout)
{
return WaitFor(timeout, () => continueFlag);
}
/// <summary>
/// </summary>
/// <param name="timeout"></param>
/// <param name="checkFlag"></param>
/// <returns>Returns <c>true</c> if operation completes before timeout</returns>
public async Task<bool> WaitFor(TimeSpan timeout, Func<bool> checkFlag)
{
double remaining = timeout.TotalMilliseconds;
while (!checkFlag())
{
if (remaining < 0)
{
//throw new TimeoutException("Timeout waiting for result for " + timeout);
return false;
}
await Task.Delay(TimeSpan.FromMilliseconds(200));
remaining -= 200;
}
return true;
}
}
}