forked from parse-community/Parse-SDK-dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCancellationToken.cs
55 lines (50 loc) · 1.58 KB
/
CancellationToken.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace System.Threading {
/// <summary>
/// A token that can be used for cancellation of an asynchronous operation.
/// </summary>
public struct CancellationToken {
private CancellationTokenSource source;
internal CancellationToken(CancellationTokenSource source) {
this.source = source;
}
/// <summary>
/// Gets an empty CancellationToken that cannot be cancelled.
/// </summary>
public static CancellationToken None {
get {
return default(CancellationToken);
}
}
/// <summary>
/// Gets whether cancellation has been requested for this token.
/// </summary>
public bool IsCancellationRequested {
get {
return source != null && source.IsCancellationRequested;
}
}
/// <summary>
/// Registers a callback to be invoked when this CancellationToken is cancelled.
/// </summary>
/// <param name="callback">The action to be invoked.</param>
/// <returns>A registration object that can be used to deregister the callback.</returns>
public CancellationTokenRegistration Register(Action callback) {
if (source != null) {
return source.Register(callback);
}
return default(CancellationTokenRegistration);
}
/// <summary>
/// Throws an OperationCanceledException if the token has been cancelled.
/// </summary>
public void ThrowIfCancellationRequested() {
if (IsCancellationRequested) {
throw new OperationCanceledException();
}
}
}
}