-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCustomTaskSource.cs
45 lines (40 loc) · 1.25 KB
/
CustomTaskSource.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
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Sources;
sealed class CustomTaskSource : IValueTaskSource
{
private readonly int every;
private long counter;
public CustomTaskSource(int every)
{
this.every = every;
}
public ValueTaskSourceStatus GetStatus(short token)
{
var increment = Interlocked.Increment(ref counter);
if (increment % every == 0)
{
Console.Write("Scheduling | ");
return ValueTaskSourceStatus.Pending;
}
Console.Write("Cached | ");
return ValueTaskSourceStatus.Succeeded;
}
public void OnCompleted(Action<object> continuation, object state, short token,
ValueTaskSourceOnCompletedFlags flags)
{
Console.Write("OnCompleted | ");
Task.Delay(Convert.ToInt32(Interlocked.Read(ref counter)) * 500)
.ConfigureAwait(flags == ValueTaskSourceOnCompletedFlags.UseSchedulingContext)
.GetAwaiter()
.OnCompleted(() =>
{
continuation(state);
Console.WriteLine("Continuation | ");
});
}
public void GetResult(short token)
{
}
}