I have this code that calculates a file md5 hash using threads and I want to know if it's possible to get the progress of this calculation so I can add a progress bar to the application (right now I have a simple animation letting the user now the application is doing something).
Here's the code of this calculation:
This code is part of the WaitForm, the form with the activity animation. The WaitForm is shown when the user loads a file in the MainForm (frmMain). The WaitForm, as you can see, is the one that will compute the file hash, set the results in the MainForm and close itself.
Is it possible to do what I want?
Here's the code of this calculation:
Visual Basic:
Delegate Sub DisplayHashCallback(ByVal text As String)
Dim fileStream As Stream
Dim hashComp As New Thread(AddressOf HashComputation)
Private Sub HashComputation()
Dim MD5 As New MD5CryptoServiceProvider
Dim bHash As Byte() = Nothing
Dim sHash As String
bHash = MD5.ComputeHash(fileStream)
sHash = BitConverter.ToString(bHash)
sHash = sHash.Replace("-", "").ToUpper
DisplayHash(sHash)
End Sub
Private Sub DisplayHash(ByVal text As String)
If frmMain.FileHash.InvokeRequired Then
Dim d As New DisplayHashCallback(AddressOf DisplayHash)
Me.Invoke(d, New Object() {text})
Else
frmMain.FileHash.Enabled = True
frmMain.FileHash.Text = text
Me.Close()
End If
End Sub
This code is part of the WaitForm, the form with the activity animation. The WaitForm is shown when the user loads a file in the MainForm (frmMain). The WaitForm, as you can see, is the one that will compute the file hash, set the results in the MainForm and close itself.
Is it possible to do what I want?