Hi @JBDayrelton ,
In a Windows Forms application, you cannot directly handle UI events like MouseDown
inside a BackgroundWorker
, as UI events are supposed to be handled on the UI thread. You can refer to the following code:
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = MouseButtons.Left Then
BackgroundWorker1.RunWorkerAsync()
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
' Perform background work here.
' You can access e.Argument to get any data you passed from the UI thread.
End Sub
If you need to pass data from the UI thread to the background worker, you can do so by setting the e.Argument
property in the MouseDown
event handler before calling RunWorkerAsync()
.
Keep in mind that if your background work involves updating the UI, you should use the ProgressChanged
event and the ReportProgress
method to communicate between the background thread and the UI thread for updating the UI in a thread-safe manner. This ensures that you don't encounter cross-threading issues.
Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.