PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Source Code/PowerSong/Progress.vb

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