IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
TaskFuture.cs
Go to the documentation of this file.
1using System;
2using System.Threading.Tasks;
3
4#if FIRST_PASS == false
5
6using java.util.concurrent;
7
8#endif
9
11{
12
13#if FIRST_PASS == false
14
18 class TaskFuture<T> : Future
19 {
20
21 readonly Task<T> task;
22 readonly Func<bool> cancellation;
23
30 public TaskFuture(Task<T> task, Func<bool> cancellation)
31 {
32 this.task = task ?? throw new ArgumentNullException(nameof(task));
33 this.cancellation = cancellation ?? throw new ArgumentNullException(nameof(cancellation));
34 }
35
41 public TaskFuture(Task<T> task)
42 {
43 this.task = task ?? throw new ArgumentNullException(nameof(task));
44 }
45
46 public bool cancel(bool mayInterruptIfRunning)
47 {
48 return cancellation != null && mayInterruptIfRunning != false && cancellation();
49 }
50
51 public bool isCancelled()
52 {
53 return task.IsCanceled;
54 }
55
56 public bool isDone()
57 {
58 return task.IsCompleted;
59 }
60
61 public object get()
62 {
63 try
64 {
65 return task.GetAwaiter().GetResult();
66 }
67 catch (OperationCanceledException e)
68 {
69 throw new CancellationException(e.Message);
70 }
71 }
72
73 public object get(long timeout, TimeUnit unit)
74 {
75 try
76 {
77 if (task.Wait(TimeSpan.FromMilliseconds(unit.toMillis(timeout))) == false)
78 throw new global::java.util.concurrent.TimeoutException();
79
80 return get();
81 }
82 catch (OperationCanceledException e)
83 {
84 throw new CancellationException(e.Message);
85 }
86 }
87
88 }
89
90#endif
91
92}
Implements the Java Future interface around a .NET Task.
Definition TaskFuture.cs:19
TaskFuture(Task< T > task)
Initializes a new instance.
Definition TaskFuture.cs:41
bool cancel(bool mayInterruptIfRunning)
Definition TaskFuture.cs:46
TaskFuture(Task< T > task, Func< bool > cancellation)
Initializes a new instance.
Definition TaskFuture.cs:30