PNRT
asked on
VB.Net BackgroundWorker
Hi Experts. I'm learning to use the backgroundworker and have completed the module below. Clicking BtnStart starts a backgroundworker which updates a progress bar and when completed enters "Process Completed" in a textbox. So far everything seems to work correctly
Please would someone be kind enough to check my code to see if I have it right but also I dont seem to be able to get the thread to cancel on a command button, I have tried entering a line in the do_work sub that checkes if cancel is pending and then using a sub with BGWorker.CancelAsync(). Howver this does not work.
Also, when the thread is running, should I be able to see an additional process in the task manager/Processes? MAny Thanks
Imports System.Threading
Module TestingBGWorker
Public WithEvents BGWorker As System.ComponentModel.Back groundWork er
Delegate Sub UpdateTextBoxDelegate()
Dim abc As Integer = 0
Public Sub StartBGWorker()
BGWorker = New System.ComponentModel.Back groundWork er
BGWorker.WorkerReportsProg ress = True
BGWorker.WorkerSupportsCan cellation = True
BGWorker.RunWorkerAsync()
End Sub
Private Sub TestWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWo rkEventArg s) Handles BGWorker.DoWork
For i = 1 To 100
Thread.Sleep(50)
abc = i
BGWorker.ReportProgress(No thing)
Next
End Sub
Private Sub TestWorker_ProgressChanged (ByVal sender As Object, ByVal e As System.ComponentModel.Prog ressChange dEventArgs ) Handles BGWorker.ProgressChanged
Form1.ProgressBar1.Visible = True
Form1.ProgressBar1.Value = abc
End Sub
Private Sub TestWorker_RunWorkerComple ted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunW orkerCompl etedEventA rgs) Handles BGWorker.RunWorkerComplete d
Form1.TextBox1.Text = "Process Complete"
Form1.ProgressBar1.Visible = False
Form1.btnCancel.Enabled = False
Form1.BtnStart.Enabled = True
End Sub
Public Sub TestWorker_CancelAsync()
Form1.btnCancel.Enabled = False
Form1.BtnStart.Enabled = True
End Sub
End Module
Please would someone be kind enough to check my code to see if I have it right but also I dont seem to be able to get the thread to cancel on a command button, I have tried entering a line in the do_work sub that checkes if cancel is pending and then using a sub with BGWorker.CancelAsync(). Howver this does not work.
Also, when the thread is running, should I be able to see an additional process in the task manager/Processes? MAny Thanks
Imports System.Threading
Module TestingBGWorker
Public WithEvents BGWorker As System.ComponentModel.Back
Delegate Sub UpdateTextBoxDelegate()
Dim abc As Integer = 0
Public Sub StartBGWorker()
BGWorker = New System.ComponentModel.Back
BGWorker.WorkerReportsProg
BGWorker.WorkerSupportsCan
BGWorker.RunWorkerAsync()
End Sub
Private Sub TestWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWo
For i = 1 To 100
Thread.Sleep(50)
abc = i
BGWorker.ReportProgress(No
Next
End Sub
Private Sub TestWorker_ProgressChanged
Form1.ProgressBar1.Visible
Form1.ProgressBar1.Value = abc
End Sub
Private Sub TestWorker_RunWorkerComple
Form1.TextBox1.Text = "Process Complete"
Form1.ProgressBar1.Visible
Form1.btnCancel.Enabled = False
Form1.BtnStart.Enabled = True
End Sub
Public Sub TestWorker_CancelAsync()
Form1.btnCancel.Enabled = False
Form1.BtnStart.Enabled = True
End Sub
End Module
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks idle, thats the sort of guidance I was looking for. I ultimately needed to get a lot of variables out from the do_work and wasnt sure how to do this without global variables. Thats also why I was going for the module. It needs to output to other than the ofrm. Any ideas how I would handle multiple cariable from the do_work? Thanks again for the reply
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Speaking of global variables, why did you decide to put this code into a Module? Since you're making everything go back to Form1 through default instance, why not have the code in the Form itself?...