PageRenderTime 22ms CodeModel.GetById 9ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/Source Code/PowerSong/Progress.vb

#
Visual Basic | 130 lines | 91 code | 38 blank | 1 comment | 0 complexity | 1f285f2a8e8271e48b4f731f9c864230 MD5 | raw file
  1Imports System.Windows.Forms
  2Imports System.Threading
  3
  4Public Class ProgressForm
  5
  6    Public MustInherit Class Task
  7
  8        Public MustOverride Sub Perform()
  9
 10        Friend Event Notify(ByVal completionPercentage As Integer, ByVal status As String)
 11        Friend Event Finished()
 12
 13        Protected Sub RaiseStatusChange(ByVal completionPercentage As Integer, Optional ByVal status As String = "")
 14            RaiseEvent Notify(completionPercentage, status)
 15        End Sub
 16
 17        Friend Sub StartTask()
 18            Try
 19                Perform()
 20            Finally
 21                RaiseEvent Finished()
 22            End Try
 23        End Sub
 24
 25        Private FCancelRequested As Boolean = False
 26
 27        Public ReadOnly Property CancelRequested() As Boolean
 28            Get
 29                Return FCancelRequested
 30            End Get
 31        End Property
 32
 33        Friend Sub Cancel()
 34            FCancelRequested = True
 35        End Sub
 36
 37    End Class
 38
 39    Private FTask As Task = Nothing
 40    Private FThread As Thread = Nothing
 41
 42    Public Sub New(ByVal task As Task, _
 43                   ByVal description As String, _
 44                   Optional ByVal initialStatus As String = "")
 45
 46        InitializeComponent()
 47        lblDescription.Text = description
 48        lblStatus.Text = initialStatus
 49        FTask = task
 50
 51    End Sub
 52
 53    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
 54
 55        If MsgBox("Are you sure you wish to cancel the operation?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.Yes Then
 56            FTask.Cancel()
 57        End If
 58
 59    End Sub
 60
 61    Private Sub HandleTaskNotification(ByVal completionPercentage As Integer, ByVal status As String)
 62        UpdateProgressBarValue(completionPercentage)
 63        UpdateStatus(status)
 64    End Sub
 65
 66    Private Sub HandleTaskFinished()
 67
 68        UpdateProgressBarValue(100)
 69        UpdateStatus("Completed.")
 70        RemoveHandler FTask.Notify, AddressOf HandleTaskNotification
 71        RemoveHandler FTask.Finished, AddressOf HandleTaskFinished
 72        CloseForm(Windows.Forms.DialogResult.OK)
 73
 74    End Sub
 75
 76    Private Sub Progress_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
 77
 78        ' Start the task
 79        AddHandler FTask.Notify, AddressOf HandleTaskNotification
 80        AddHandler FTask.Finished, AddressOf HandleTaskFinished
 81        FThread = New Thread(AddressOf FTask.StartTask)
 82        FThread.Start()
 83
 84    End Sub
 85
 86#Region " Enables Cross-Thread Updating of Controls "
 87
 88    Delegate Sub SameThreadUpdateStatusDelegate(ByVal status As String)
 89    Delegate Sub SameThreadUpdateProgressBarValueDelegate(ByVal value As Double)
 90    Delegate Sub SameThreadCloseFormDelegate(ByVal result As DialogResult)
 91
 92    Friend Sub UpdateStatus(ByRef status As String)
 93
 94        If Me.Visible Then
 95            Dim Args As Object() = New Object(0) {status}
 96            Me.Invoke(New SameThreadUpdateStatusDelegate(AddressOf SameThreadUpdateStatus), Args)
 97        End If
 98
 99    End Sub
100
101    Friend Sub UpdateProgressBarValue(ByRef value As Double)
102
103        If Me.Visible Then
104            Dim Args As Object() = New Object(0) {value}
105            Me.Invoke(New SameThreadUpdateProgressBarValueDelegate(AddressOf SameThreadUpdateProgressBarValue), Args)
106        End If
107
108    End Sub
109
110    Friend Sub CloseForm(ByVal result As DialogResult)
111        Dim Args As Object() = New Object(0) {result}
112        Me.Invoke(New SameThreadCloseFormDelegate(AddressOf SameThreadCloseForm), result)
113    End Sub
114
115    Private Sub SameThreadUpdateStatus(ByVal status As String)
116        lblStatus.Text = status
117    End Sub
118
119    Private Sub SameThreadUpdateProgressBarValue(ByVal value As Double)
120        pbProgress.Value = CInt(value * 10)
121    End Sub
122
123    Private Sub SameThreadCloseForm(ByVal result As DialogResult)
124        Me.DialogResult = result
125        Me.Close()
126    End Sub
127
128#End Region
129
130End Class