1
1
using System ;
2
+ using System . Diagnostics . CodeAnalysis ;
2
3
using System . Threading . Tasks ;
3
4
4
5
namespace Open . Threading . Tasks
5
6
{
6
7
public class AsyncQuery < TResult > : AsyncProcess
7
8
{
8
- TResult _latest ;
9
+ #if NETSTANDARD2_1
10
+ [ AllowNull ]
11
+ #endif
12
+ TResult _latest = default ! ;
9
13
10
- protected new Func < Progress , TResult > Closure
14
+ protected new Func < Progress , TResult > ? Closure
11
15
{
12
16
get ;
13
17
private set ;
14
18
}
15
19
16
- protected Task < TResult > InternalTaskValued
20
+ protected Task < TResult > ? InternalTaskValued
17
21
{
18
22
get ;
19
23
private set ;
20
24
}
21
25
22
- public AsyncQuery ( Func < Progress , TResult > query , TaskScheduler scheduler = null )
23
- : base ( null , scheduler )
26
+ public AsyncQuery ( Func < Progress , TResult > query , TaskScheduler ? scheduler = null )
27
+ : base ( scheduler )
24
28
{
25
- Closure = query ;
29
+ Closure = query ?? throw new ArgumentNullException ( nameof ( query ) ) ;
26
30
}
27
31
28
32
protected Task < TResult > EnsureProcessValued ( bool once , TimeSpan ? timeAllowedBeforeRefresh = null )
29
33
{
30
34
31
- Task < TResult > task = null ;
35
+ Task < TResult > ? task = null ;
32
36
33
- SyncLock . ReadWriteConditionalOptimized (
37
+ SyncLock ! . ReadWriteConditionalOptimized (
34
38
write =>
35
39
{
36
40
task = InternalTaskValued ;
37
- return ( task == null || ! once && ! task . IsActive ( ) ) // No action, or completed?
41
+ return ( task is null || ! once && ! task . IsActive ( ) ) // No action, or completed?
38
42
&& ( ! timeAllowedBeforeRefresh . HasValue // Now?
39
43
|| timeAllowedBeforeRefresh . Value < DateTime . Now - LatestCompleted ) ; // Or later?
40
44
} , ( ) =>
@@ -49,44 +53,45 @@ protected Task<TResult> EnsureProcessValued(bool once, TimeSpan? timeAllowedBefo
49
53
) ;
50
54
51
55
// action could be null in some cases where timeAllowedBeforeRefresh condition is still met.
52
- return task ;
56
+ return task ! ;
53
57
}
54
58
55
59
protected override Task EnsureProcess ( bool once , TimeSpan ? timeAllowedBeforeRefresh = null )
56
- {
57
- return EnsureProcessValued ( once , timeAllowedBeforeRefresh ) ;
58
- }
60
+ => EnsureProcessValued ( once , timeAllowedBeforeRefresh ) ;
59
61
60
62
//long _processCount = 0;
63
+ #if NETSTANDARD2_1
64
+ [ return : MaybeNull ]
65
+ #endif
61
66
protected new TResult Process ( object progress )
62
67
{
63
68
64
69
var p = ( Progress ) progress ;
65
70
try
66
71
{
67
72
//Contract.Assert(Interlocked.Increment(ref _processCount) == 1);
68
- var result = Closure ( p ) ;
73
+ var result = Closure ! ( p ) ;
69
74
Latest = result ;
70
75
return result ;
71
76
}
72
77
catch ( Exception ex )
73
78
{
74
- SyncLock . Write ( ( ) => LatestCompleted = DateTime . Now ) ;
79
+ SyncLock ! . Write ( ( ) => LatestCompleted = DateTime . Now ) ;
75
80
p . Failed ( ex . ToString ( ) ) ;
76
81
}
77
82
//finally
78
83
//{
79
84
// //Interlocked.Decrement(ref _processCount);
80
85
//}
81
- return default ;
86
+ return default ! ;
82
87
}
83
88
84
89
public bool IsCurrentDataReady
85
90
{
86
91
get
87
92
{
88
93
var t = InternalTask ;
89
- if ( t == null )
94
+ if ( t is null )
90
95
return false ;
91
96
return ! t . IsActive ( ) ;
92
97
}
@@ -124,7 +129,7 @@ protected virtual TResult GetLatest()
124
129
125
130
public virtual void OverrideLatest ( TResult value , DateTime ? completed = null )
126
131
{
127
- SyncLock . Write ( ( ) =>
132
+ SyncLock ! . Write ( ( ) =>
128
133
{
129
134
_latest = value ;
130
135
LatestCompleted = completed ?? DateTime . Now ;
@@ -134,7 +139,7 @@ public virtual void OverrideLatest(TResult value, DateTime? completed = null)
134
139
135
140
public virtual void OverrideLatest ( TResult value , Func < TResult , TResult , bool > useNewValueEvaluator , DateTime ? completed = null )
136
141
{
137
- SyncLock . ReadWriteConditionalOptimized (
142
+ SyncLock ! . ReadWriteConditionalOptimized (
138
143
( write ) => useNewValueEvaluator ( _latest , value ) ,
139
144
( ) =>
140
145
{
@@ -155,8 +160,8 @@ public TResult Latest
155
160
156
161
public bool WaitForRunningToComplete ( TimeSpan ? waitForCurrentTimeout = null )
157
162
{
158
- var task = SyncLock . ReadValue ( ( ) => InternalTaskValued ) ;
159
- if ( task == null ) return false ;
163
+ var task = SyncLock ! . ReadValue ( ( ) => InternalTaskValued ) ;
164
+ if ( task is null ) return false ;
160
165
if ( waitForCurrentTimeout . HasValue )
161
166
task . Wait ( waitForCurrentTimeout . Value ) ;
162
167
else
@@ -169,8 +174,8 @@ public TResult RunningValue
169
174
{
170
175
get
171
176
{
172
- var task = SyncLock . ReadValue ( ( ) => InternalTaskValued ) ;
173
- return task == null ? GetRunningValue ( ) : task . Result ;
177
+ var task = SyncLock ! . ReadValue ( ( ) => InternalTaskValued ) ;
178
+ return task is null ? GetRunningValue ( ) : task . Result ;
174
179
}
175
180
}
176
181
@@ -186,17 +191,22 @@ public TResult ActiveRunningValueOrLatestPossible
186
191
}
187
192
}
188
193
189
- public virtual bool TryGetLatest ( out TResult latest , out DateTime completed )
194
+ public virtual bool TryGetLatest (
195
+ #if NETSTANDARD2_1
196
+ [ NotNullWhen ( true ) ]
197
+ #endif
198
+ out TResult latest ,
199
+ out DateTime completed )
190
200
{
191
201
var result = default ( TResult ) ;
192
202
var resultComplete = DateTime . MinValue ;
193
- var isReady = SyncLock . ReadValue ( ( ) =>
203
+ var isReady = SyncLock ! . ReadValue ( ( ) =>
194
204
{
195
205
result = _latest ;
196
206
resultComplete = LatestCompleted ;
197
207
return IsLatestAvailable ;
198
208
} ) ;
199
- latest = result ;
209
+ latest = result ! ;
200
210
completed = resultComplete ;
201
211
return isReady ;
202
212
}
@@ -255,7 +265,7 @@ public TResult GetLatestOrRunning(out DateTime completed)
255
265
protected override void OnDispose ( )
256
266
{
257
267
base . OnDispose ( ) ;
258
- _latest = default ;
268
+ _latest = default ! ;
259
269
Closure = null ;
260
270
}
261
271
}
0 commit comments