-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuture.cs
66 lines (56 loc) · 1.54 KB
/
Future.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
namespace Dwolla.Core
{
using System;
using System.Threading;
public class Future<T>
{
public Func<T> Expression { get; private set; }
public bool Complete { get; private set; }
private T _value;
public T Value
{
get {
if (!Complete)
{
Thread.Sleep(10);
}
return _value;
}
private set { _value = value; }
}
protected Exception Error { get; set; }
public Future(Func<T> expression)
{
Expression = expression;
Expression.BeginInvoke(CompleteCallback, null);
}
private void CompleteCallback(IAsyncResult ar)
{
try
{
Value = Expression.EndInvoke(ar);
}
catch (Exception ex)
{
Error = ex;
}
Complete = true;
}
public static implicit operator T(Future<T> future)
{
return future.Value;
}
public static implicit operator Future<T>(T value)
{
return new Future<T>(() => value);
}
public static implicit operator Future<T>(Func<T> expression)
{
return new Future<T>(expression);
}
public static implicit operator Func<T>(Future<T> future)
{
return () => future.Value;
}
}
}