/Source Code/PowerSong/Progress.vb
Visual Basic | 130 lines | 91 code | 38 blank | 1 comment | 0 complexity | 1f285f2a8e8271e48b4f731f9c864230 MD5 | raw file
- Imports System.Windows.Forms
- Imports System.Threading
-
- Public Class ProgressForm
-
- Public MustInherit Class Task
-
- Public MustOverride Sub Perform()
-
- Friend Event Notify(ByVal completionPercentage As Integer, ByVal status As String)
- Friend Event Finished()
-
- Protected Sub RaiseStatusChange(ByVal completionPercentage As Integer, Optional ByVal status As String = "")
- RaiseEvent Notify(completionPercentage, status)
- End Sub
-
- Friend Sub StartTask()
- Try
- Perform()
- Finally
- RaiseEvent Finished()
- End Try
- End Sub
-
- Private FCancelRequested As Boolean = False
-
- Public ReadOnly Property CancelRequested() As Boolean
- Get
- Return FCancelRequested
- End Get
- End Property
-
- Friend Sub Cancel()
- FCancelRequested = True
- End Sub
-
- End Class
-
- Private FTask As Task = Nothing
- Private FThread As Thread = Nothing
-
- Public Sub New(ByVal task As Task, _
- ByVal description As String, _
- Optional ByVal initialStatus As String = "")
-
- InitializeComponent()
- lblDescription.Text = description
- lblStatus.Text = initialStatus
- FTask = task
-
- End Sub
-
- Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
-
- If MsgBox("Are you sure you wish to cancel the operation?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.Yes Then
- FTask.Cancel()
- End If
-
- End Sub
-
- Private Sub HandleTaskNotification(ByVal completionPercentage As Integer, ByVal status As String)
- UpdateProgressBarValue(completionPercentage)
- UpdateStatus(status)
- End Sub
-
- Private Sub HandleTaskFinished()
-
- UpdateProgressBarValue(100)
- UpdateStatus("Completed.")
- RemoveHandler FTask.Notify, AddressOf HandleTaskNotification
- RemoveHandler FTask.Finished, AddressOf HandleTaskFinished
- CloseForm(Windows.Forms.DialogResult.OK)
-
- End Sub
-
- Private Sub Progress_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
-
- ' Start the task
- AddHandler FTask.Notify, AddressOf HandleTaskNotification
- AddHandler FTask.Finished, AddressOf HandleTaskFinished
- FThread = New Thread(AddressOf FTask.StartTask)
- FThread.Start()
-
- End Sub
-
- #Region " Enables Cross-Thread Updating of Controls "
-
- Delegate Sub SameThreadUpdateStatusDelegate(ByVal status As String)
- Delegate Sub SameThreadUpdateProgressBarValueDelegate(ByVal value As Double)
- Delegate Sub SameThreadCloseFormDelegate(ByVal result As DialogResult)
-
- Friend Sub UpdateStatus(ByRef status As String)
-
- If Me.Visible Then
- Dim Args As Object() = New Object(0) {status}
- Me.Invoke(New SameThreadUpdateStatusDelegate(AddressOf SameThreadUpdateStatus), Args)
- End If
-
- End Sub
-
- Friend Sub UpdateProgressBarValue(ByRef value As Double)
-
- If Me.Visible Then
- Dim Args As Object() = New Object(0) {value}
- Me.Invoke(New SameThreadUpdateProgressBarValueDelegate(AddressOf SameThreadUpdateProgressBarValue), Args)
- End If
-
- End Sub
-
- Friend Sub CloseForm(ByVal result As DialogResult)
- Dim Args As Object() = New Object(0) {result}
- Me.Invoke(New SameThreadCloseFormDelegate(AddressOf SameThreadCloseForm), result)
- End Sub
-
- Private Sub SameThreadUpdateStatus(ByVal status As String)
- lblStatus.Text = status
- End Sub
-
- Private Sub SameThreadUpdateProgressBarValue(ByVal value As Double)
- pbProgress.Value = CInt(value * 10)
- End Sub
-
- Private Sub SameThreadCloseForm(ByVal result As DialogResult)
- Me.DialogResult = result
- Me.Close()
- End Sub
-
- #End Region
-
- End Class