-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleMTProgress.xaml.cs
More file actions
115 lines (103 loc) · 3.99 KB
/
Copy pathSimpleMTProgress.xaml.cs
File metadata and controls
115 lines (103 loc) · 3.99 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
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
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
namespace SimpleProgressWindow
{
public partial class SimpleMTProgress : Window
{
public string GetElapsedTime() { return $"{sw.Elapsed.Minutes:00}:{sw.Elapsed.Seconds:00}"; }
private ESAPIWorker slave;
private SimpleMTbase callerClass;
//get instances of the stopwatch and dispatch timer to report how long the calculation takes at each reporting interval
private Stopwatch sw = new Stopwatch();
private DispatcherTimer dt = new DispatcherTimer(DispatcherPriority.Normal);
private bool _closeOnSuccessfulFinish = false;
private int _closeTimeOut = 3000;
public SimpleMTProgress()
{
InitializeComponent();
}
//template function
public void SetCallerClass<T>(ESAPIWorker e, T caller)
{
//Make all worker classes derive from MTbase. This simplifies the type casting as opposed to try and figure out the class at run time
callerClass = caller as SimpleMTbase;
slave = e;
//initialize and start the stopwatch
dt.Tick += new EventHandler(Dt_tick);
dt.Interval = new TimeSpan(0, 0, 0, 0, 100);
DoStuff();
}
private void Dt_tick(object sender, EventArgs e)
{
//increment the time on the progress window for each "tick", which is set to intervals of 1 second
if (sw.IsRunning)
{
TimeSpan ts = sw.Elapsed;
runTime.Text = $"{ts.Minutes:00}:{ts.Seconds:00}";
}
}
public void DoStuff()
{
slave.DoWork(() =>
{
//get instance of current dispatcher which is used to asynchronously execute tasks on the MT progress UI thread
Dispatcher dispatch = Dispatcher;
//asign the dispatcher and an instance of this class to the caller class (used to marshal updates back to this UI)
callerClass.SetDispatcherAndUIInstance(dispatch, this);
//start the stopwatch
sw.Start();
dt.Start();
//start the tasks asynchronously
if (callerClass.Run()) slave.isError = true;
//stop the stopwatch
sw.Stop();
dt.Stop();
if (_closeOnSuccessfulFinish && !slave.isError) HandleCloseOnFinish(dispatch);
});
}
public void UpdateLabel(string message)
{
taskLabel.Text = message;
}
public void ProvideUpdate(int percentComplete, string message = "", bool fail = false)
{
if (fail) FailEvent();
progress.Value = percentComplete;
if (!string.IsNullOrEmpty(message))
{
progressTB.Text += message + Environment.NewLine;
scroller.ScrollToBottom();
}
}
public void ProvideUpdate(string message, bool fail = false)
{
if (fail) FailEvent();
progressTB.Text += message + Environment.NewLine;
scroller.ScrollToBottom();
}
public void SetCloseOnFinish(bool closeOnFinish, int timeout)
{
_closeOnSuccessfulFinish = closeOnFinish;
_closeTimeOut = timeout;
}
private void HandleCloseOnFinish(Dispatcher dispatch)
{
dispatch.Invoke((Action)(() => { Thread.Sleep(_closeTimeOut); }));
dispatch.Invoke((Action)(() => { this.Close(); }));
}
private void FailEvent()
{
progress.Background = Brushes.Red;
progress.Foreground = Brushes.Red;
UpdateLabel("FAILED!");
}
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
SizeToContent = SizeToContent.WidthAndHeight;
}
}
}