blob: e06c2f95bf65d4c9e071bc44b99882e69c4d2b59 (
plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
namespace Tango.FSE.Common.RemoteUpgrade
{
public class RemoteUpgradeHandler : ExtendedObject
{
private Action _abortAction;
private TaskCompletionSource<RemoteUpgradeHandlerStatus> _completionSource;
public event EventHandler<RemoteUpgradeHandlerStatus> StatusChanged;
public event EventHandler<TangoProgressChangedEventArgs<double>> ProgressChanged;
internal bool IsAborted { get; private set; }
public bool CanAbort { get; internal set; } = true;
private TangoProgress<double> _progress;
public TangoProgress<double> Progress
{
get { return _progress; }
set { _progress = value; RaisePropertyChangedAuto(); }
}
private RemoteUpgradeHandlerStatus _status;
public RemoteUpgradeHandlerStatus Status
{
get { return _status; }
set
{
if (_status != value)
{
_status = value;
RaisePropertyChangedAuto();
StatusChanged?.Invoke(this, value);
}
}
}
private Exception _failedException;
public Exception FailedException
{
get { return _failedException; }
set { _failedException = value; RaisePropertyChangedAuto(); }
}
public RemoteUpgradeHandler()
{
Progress = new TangoProgress<double>()
{
Message = "Initializing...",
Maximum = 100,
};
_completionSource = new TaskCompletionSource<RemoteUpgradeHandlerStatus>();
}
public RemoteUpgradeHandler(String message) : this()
{
Progress.Message = message;
Progress.Maximum = 100;
}
internal RemoteUpgradeHandler(Action abortAction) : this()
{
_abortAction = abortAction;
}
internal void UpdateProgress(String message, bool isIndeterminate = true, double progress = 0, double maximum = 100)
{
UpdateProgress(new TangoProgress<double>()
{
Value = progress,
Maximum = maximum,
IsIndeterminate = isIndeterminate,
Message = message,
});
}
internal void UpdateProgress(TangoProgress<double> progress)
{
Progress = progress;
ProgressChanged?.Invoke(this, new TangoProgressChangedEventArgs<double>() { Progress = Progress });
}
public void Abort()
{
IsAborted = true;
_abortAction?.Invoke();
}
public Task WaitForCompletion()
{
return _completionSource.Task;
}
internal void RaiseFailed(Exception exception)
{
FailedException = exception;
Status = RemoteUpgradeHandlerStatus.Failed;
CanAbort = true;
_completionSource.SetException(exception);
}
internal void RaiseCompleted()
{
Status = RemoteUpgradeHandlerStatus.Completed;
CanAbort = true;
_completionSource.SetResult(Status);
}
internal void RaiseAborted()
{
Status = RemoteUpgradeHandlerStatus.Aborted;
CanAbort = true;
_completionSource.SetException(new OperationCanceledException("Remote upgrade operation aborted."));
}
}
}
|