-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExecProgress.vb
64 lines (58 loc) · 2.5 KB
/
ExecProgress.vb
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
Imports System.Threading
Imports WSLMan.My.Resources
Public Class ExecProgress
Private IsProcessComplete As Boolean = False
Private CancellationTokenSource As CancellationTokenSource
Private _exitCode As Integer? = Nothing
Private CommandLine As String
Public Sub ShowExecute(Command As String, Arguments As String, Optional PauseOnFail As Boolean = False)
IsProcessComplete = False
CancellationTokenSource = New CancellationTokenSource()
CommandLine = $"{Command} {Arguments}"
_exitCode = Nothing
ExecCommandSimple(
Command,
Arguments,
PauseOnFail,
CancellationTokenSource.Token
).ContinueWith(Sub(t)
If Not Created Then
_exitCode = t.Result
Exit Sub
End If
Invoke(Sub() OnComplete(t.Result))
End Sub)
ShowDialog()
End Sub
Private Sub OnComplete(ExitCode As Integer)
IsProcessComplete = True
TextBoxProgress.Text = String.Format(Resources.ProcessExitedWithCode, ExitCode, CommandLine)
TextBoxProgress.SelectionStart = 0
TextBoxProgress.SelectionLength = 0
TextBoxProgress.ScrollToCaret()
ButtonAbort.Text = Resources.CloseText
If ExitCode = 0 Then ButtonAbort.Focus()
End Sub
Private Sub ExecProgress_Load(sender As Object, e As EventArgs) Handles Me.Load
TextBoxProgress.Text = String.Format(Resources.ExecutingMessage, CommandLine)
TextBoxProgress.SelectionStart = 0
TextBoxProgress.SelectionLength = 0
TextBoxProgress.ScrollToCaret()
If _exitCode IsNot Nothing Then
OnComplete(CInt(_exitCode))
End If
End Sub
Private Sub ButtonAbort_Click(sender As Object, e As EventArgs) Handles ButtonAbort.Click
Close()
End Sub
Private Sub ExecProgress_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If Not IsProcessComplete Then
If MessageBox.Show(Resources.AbortProcessPrompt, GetApplicationName(), MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) <> DialogResult.Yes Then
e.Cancel = True
Exit Sub
End If
CancellationTokenSource.Cancel()
e.Cancel = True
End If
End Sub
End Class