-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathTaskHandle.cs
More file actions
76 lines (67 loc) · 2.33 KB
/
Copy pathTaskHandle.cs
File metadata and controls
76 lines (67 loc) · 2.33 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MW5.Plugins.Interfaces;
using MW5.Shared.Log;
namespace MW5.Tools.Model
{
/// <summary>
/// Represents a handle of the task that is passed to the IGisTool.Run method to report progress and
/// respond to the user actions.
/// </summary>
public class TaskHandle : ITaskHandle
{
private CancellationToken _cancellationToken;
/// <summary>
/// Initializes a new instance of the <see cref="TaskHandle"/> class.
/// </summary>
public TaskHandle(ITaskProgress progress, CancellationToken cancellationToken, WaitHandle pauseHandle,
IGlobalListener callback)
{
if (progress == null) throw new ArgumentNullException("progress");
if (pauseHandle == null) throw new ArgumentNullException("pauseHandle");
if (callback == null) throw new ArgumentNullException("callback");
Callback = callback;
PauseHandle = pauseHandle;
Progress = progress;
_cancellationToken = cancellationToken;
}
/// <summary>
/// Gets the callback.
/// </summary>
public IGlobalListener Callback { get; private set; }
/// <summary>
/// Gets the progress.
/// </summary>
public ITaskProgress Progress { get; private set; }
/// <summary>
/// Gets the pause handle.
/// </summary>
public WaitHandle PauseHandle { get; private set; }
/// <summary>
/// Gets a value indicating whether the task is cancelled.
/// </summary>
public bool IsCancelled
{
get { return _cancellationToken.IsCancellationRequested; }
}
/// <summary>
/// Aborts async execution by using cancellation token if user has canceled the execution via UI.
/// </summary>
public void AbortIfCancelled()
{
_cancellationToken.ThrowIfCancellationRequested();
}
/// <summary>
/// Checks if the execution should be paused or canceled.
/// </summary>
public void CheckPauseAndCancel()
{
AbortIfCancelled();
PauseHandle.WaitOne();
}
}
}