-
Notifications
You must be signed in to change notification settings - Fork 0
/
Operation.cs
110 lines (96 loc) · 3.11 KB
/
Operation.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace DNQ.VirtualizedItemsSource
{
internal partial class Operation<T>
{
private readonly object _synchObject = new object();
private volatile bool _wasCancelled = false;
private volatile bool _wasSuccessful = false;
private static int _operationsCount = 0;
private Action<OperationResult<T>> _callback;
private CancellationTokenSource _cancellationSource;
public Operation(OperationType opType, int start, int end, int span, IBounds originalBounds, int originalTotalCount, Action<IOperationResult<T>> callback)
{
OperationType = opType; Start = start; End = end; Span = span;
OriginalBounds = originalBounds;
OriginalTotalCount = originalTotalCount;
_callback = callback;
_cancellationSource = new CancellationTokenSource();
OperationNumber = Interlocked.Increment(ref _operationsCount);
}
public int OperationNumber { get; private set; }
public OperationType OperationType { get; private set; }
public int Start { get; private set; }
public int End { get; private set; }
public int Span { get; private set; }
public IBounds OriginalBounds { get; private set; }
public int OriginalTotalCount { get; private set; }
public bool WasCancelled
{
get
{
lock (_synchObject)
{
return _wasCancelled;
}
}
}
public bool WasSuccessful
{
get
{
lock (_synchObject)
{
return _wasSuccessful;
}
}
}
public void Cancel()
{
lock (_synchObject)
{
_wasCancelled = true;
_cancellationSource.Cancel();
}
}
public CancellationToken CancellationToken
{
get
{
return _cancellationSource.Token;
}
}
public void SetComplete(T[] array, IBounds b, bool mustRefresh)
{
lock (_synchObject)
{
_wasSuccessful = true;
}
if (_callback != null)
{
OperationResult<T> opResult = new OperationResult<T>(this, array, b, mustRefresh);
_callback(opResult);
}
}
public void SetComplete(Exception exc)
{
lock (_synchObject)
{
_wasSuccessful = false;
}
if (_callback != null)
{
OperationResult<T> opResult = new OperationResult<T>(this, exc);
_callback(opResult);
}
}
public override string ToString()
{
return string.Format("{0} #{1}: From {2} to {3}, Span = {4}", OperationType, OperationNumber, Start, End, Span);
}
}
}