|
-
Oct 25th, 2013, 05:41 AM
#5
Re: BackroundWorker Problem that I can't figure out
 Originally Posted by .paul.
here's an example:
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For x As Integer = 1 To 10
setText(x.ToString)
MsgBox(getText)
Next
End Sub
Private Delegate Sub setTextCallBack(ByVal newValue As String)
Private Sub setText(ByVal newValue As String)
If TextBox1.InvokeRequired Then
TextBox1.Invoke(New setTextCallBack(AddressOf setText), newValue)
Else
TextBox1.Text = newValue
End If
End Sub
Private Delegate Function getTextCallBack() As String
Private Function getText() As String
If TextBox1.InvokeRequired Then
Return DirectCast(TextBox1.Invoke(New getTextCallBack(AddressOf getText)), String)
Else
Return TextBox1.Text
End If
End Function
End Class
While this would work, this seems to defeat the whole purpose of the BackgroundWorker class. The background worker class was really meant to avoid this type of code. It has all the logic already built into it.
Whenever you want to access any UI element, do that via the ReportProgress method and ProgressChanged event. You can pass the ProgressPercentage to the ProgressReport method, telling it how much of your work is complete. Additionally you can pass it the UserState parameter with whatever value you want and do UI related things with that, if required. These two things are available for use in the ProgressChanged event handler.
Here is an example of how to do it the proper way, avoiding delegates:
Put a TextBox, a ProgressBar, and two Buttons on a form and the following code.
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ProgressBar1.Maximum = 100 ProgressBar1.Value = 0 BackgroundWorker1.WorkerReportsProgress = True BackgroundWorker1.WorkerSupportsCancellation = True BackgroundWorker1.RunWorkerAsync() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ' Cancel button BackgroundWorker1.CancelAsync() End Sub Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Const MAX As Integer = 1000 For i As Integer = 1 To MAX ' some delay to simulate work in progress Threading.Thread.Sleep(100) ' report the progress to UI. You can optionally pass any values required to be accessed by UI elements. Dim progress As Integer = (i * 100) \ MAX BackgroundWorker1.ReportProgress(progress, i) ' check if user wants to cancel this task. If Yes, then quit. If BackgroundWorker1.CancellationPending Then e.Cancel = True Exit Sub End If Next End Sub Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged ProgressBar1.Value = e.ProgressPercentage TextBox1.Text = e.UserState.ToString End Sub Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted If e.Cancelled Then MessageBox.Show("Cancelled!") Else MessageBox.Show("Work done") End If End Sub
Run the program and first click Button1. It will start the background worker thread. Click Button2 anywhere in between to cancel it.
Last edited by Pradeep1210; Oct 25th, 2013 at 09:24 AM.
Reason: Corrected grammar and explained in detail.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|