The reason should be fairly obvious:
Private Sub EncyptFiles()
If Me.InvokeRequired Then
Me.Invoke(New EncyptFilesDelegate(AddressOf EncyptFiles), New Object() {})
Else
For Each Filename As String In frmMain.OpenFileDialog1.FileNames
FileCrypt(Filename, save & folders(Index) & "\" & file, UniqueID, Crypt.CryptAction.Encrypt)
Next
End If
End Sub
- Your
BackgroundWorker
kicks off a new thread to raise the DoWork
event; - The
DoWork
event handler calls EncryptFiles
; - The
EncryptFiles
method notices that it's running on a background thread, and calls Invoke
to move the work back to the UI thread; - Work continues on the UI thread.
The solution is fairly simple:
- Remove the
InvokeRequired
/ Invoke
code from your EncryptFiles
method. - Use the
BackgroundWorker
's ReportProgress
method[^] to pass the % progress and text back to the UI; - Handle the
BackgroundWorker
's ProgressChanged
event[^] to update text and progress bar.