-
Jun 1st, 2007, 02:57 AM
#1
Using the BackgroundWorker Component
C# version here.
Create a new Windows Forms project. Add a Label, a ProgressBar and a BackgroundWorker to the form. Set the BackgroundWorker's WorkerReportsProgress property to True. Add the following code then run the project.
VB.NET Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Raise the DoWork event in a worker thread. Me.BackgroundWorker1.RunWorkerAsync() End Sub 'This method is executed in a worker thread. Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _ ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker) For i As Integer = 1 To 100 'Raise the ProgressChanged event in the UI thread. worker.ReportProgress(i, i & " iterations complete") 'Perform some time-consuming operation here. Threading.Thread.Sleep(250) Next i End Sub 'This method is executed in the UI thread. Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _ ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged Me.ProgressBar1.Value = e.ProgressPercentage Me.Label1.Text = TryCast(e.UserState, String) End Sub 'This method is executed in the UI thread. Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _ ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted Me.Label1.Text = "Operation complete" End Sub
Last edited by jmcilhinney; Oct 28th, 2008 at 10:49 PM.
-
Jun 3rd, 2007, 06:07 AM
#2
Passing Data to be Used in the Worker Thread.
E.g. Perform a loop in the worker thread with a number of iterations determined by the value set in a NumericUpDown:
VB.NET Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.BackgroundWorker1.RunWorkerAsync(CInt(Me.NumericUpDown1.Value))
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim worker As BackgroundWorker = DirectCast(sender, BackgroundWorker)
Dim iterationCount As Integer = 0
If TypeOf e.Argument Is Integer Then
iterationCount = CInt(e.Argument)
End If
For i As Integer = 1 To iterationCount Step 1
'Do something here.
Next i
End Sub
-
Jul 4th, 2007, 03:03 AM
#3
Re: Using the BackgroundWorker Component
How to fill multiple combo using the background worker component!
I done this for a single combo but for the multiple combo it is not working!
Please guide me
VB.NET Code:
Private Sub BackgroundWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker.DoWork
Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
rawMaterialCategoryDataTable = rawMaterialCategory.ReturnDataTable(, "ORDER BY CategoryName") 'Returnning a Datatable
Dim dataColumn As New DataColumn
dataColumn.DataType = GetType(Integer)
dataColumn.ColumnName = "SearchingCode"
Dim searchingCode As Integer
rawMaterialCategoryDataTable.Columns.Add(dataColumn)
For Each dataRow As DataRow In rawMaterialCategoryDataTable.Rows
worker.ReportProgress(0, dataRow("CategoryName"))
rawMaterialCategoryDataTable.Rows(searchingCode).Item("SearchingCode") = searchingCode + 1
searchingCode += 1
Next dataRow
End Sub
Private Sub BackgroundWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker.ProgressChanged
If Not e.UserState Is Nothing Then
Me.sRawMaterialCategory.Items.Add(CStr(e.UserState).Trim) 'Adding In the combo Box
Else
'Me.ProgressBar1.Value = e.ProgressPercentage
End If
End Sub
-
Nov 9th, 2007, 10:59 PM
#4
Re: Using the BackgroundWorker Component
Here are two examples that contrast using a BackgroundWorker to update the UI as the work is done and using it to update the UI only once the work is complete:
vb.net Code:
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _ ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim worker As BackgroundWorker = DirectCast(sender, BackgroundWorker) For Each filePath As String In IO.Directory.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyDocuments) worker.ReportProgress(0, IO.Path.GetFileName(filePath)) Next filePath End Sub Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _ ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged Dim fileName As String = DirectCast(e.UserState, String) Me.ListBox1.Items.Add(fileName) End Sub
vb.net Code:
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _ ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim worker As BackgroundWorker = DirectCast(sender, BackgroundWorker) Dim fileNames As String() = IO.Directory.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyDocuments) For index As Integer = 0 To fileNames.GetUpperBound(0) Step 1 fileNames(index) = IO.Path.GetFileName(fileNames(index)) Next index e.Result = fileNames End Sub Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _ ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted Dim fileNames As String() = DirectCast(e.Result, String()) Me.ListBox1.Items.AddRange(fileNames) End Sub
These examples show how you can pass data to the UI from a BackgroundWorker in two different ways without having to explicitly delegate, which is one of the first things everyone has trouble with when multi-threading.
Note that for the first example to work the WorkerReportsProgress property must be set to True.
Last edited by jmcilhinney; Feb 12th, 2008 at 12:44 AM.
-
Apr 2nd, 2008, 10:12 PM
#5
Addicted Member
Re: Using the BackgroundWorker Component
I am trying to follow your first post and I get this error:
"This BackgroundWorker states that it doesn't report progress. Modify WorkerReportsProgress to state that it does report progress."
Everything works okay when I comment out the line that errors...
Do you have the quick solution to this problem?
Nicely done all the same. Cheers.
-
Apr 2nd, 2008, 10:37 PM
#6
Re: Using the BackgroundWorker Component
Originally Posted by scootabug
I am trying to follow your first post and I get this error:
"This BackgroundWorker states that it doesn't report progress. Modify WorkerReportsProgress to state that it does report progress."
Everything works okay when I comment out the line that errors...
Do you have the quick solution to this problem?
Nicely done all the same. Cheers.
The error message says this:
Modify WorkerReportsProgress to state that it does report progress.
In post #1 I said this:
Set the BackgroundWorker's WorkerReportsProgress property to True.
In post #4 I said this:
Note that for the first example to work the WorkerReportsProgress property must be set to True.
Are you noticing a theme here?
-
Apr 3rd, 2008, 12:40 AM
#7
Addicted Member
Re: Using the BackgroundWorker Component
Sorry, my bad. I noticed the property after I'd posted, hadn't got a chance to come back here until now to say 'nevermind!'. Thanks though, you are invaluable.
-
May 28th, 2008, 12:45 AM
#8
Lively Member
Re: Using the BackgroundWorker Component
thanks for the outline., it helped me understand the concept of using dowork with triggering RunWorkerCompleted and progresschanged..
question... this was posted in 2007.. im guessing it has not really for .net 3.5... is it?
now., is it possible to tie in the background worker with the calls to a webservice? im sure there is.. i know i have seen the async calls somewhere., possibly with the buffer = false?.. i ask because... i have a handheld device, that queries a webservice for a dataset, the webservice generates the dataset, and it has around 20,000 records... surprisingly., it generates the data in about a minute,. and then the device inserts the data into its sqlce db.. so, im wondering if there is a way to turn off buffering on the webserice call and populate the database as data comes back... rather than., send command-> wait for entire dataset -> insert data set...... i want to do a send command -> wait a bit, ->do a bit -> loop a bit
sadly i have to communicate thru a web service... client needs top security and will not allow access to db's directly from desktop/work station... all db's must be accessed thru web serivce...
-
May 28th, 2008, 01:09 AM
#9
Re: Using the BackgroundWorker Component
Originally Posted by zxed
thanks for the outline., it helped me understand the concept of using dowork with triggering RunWorkerCompleted and progresschanged..
question... this was posted in 2007.. im guessing it has not really for .net 3.5... is it?
now., is it possible to tie in the background worker with the calls to a webservice? im sure there is.. i know i have seen the async calls somewhere., possibly with the buffer = false?.. i ask because... i have a handheld device, that queries a webservice for a dataset, the webservice generates the dataset, and it has around 20,000 records... surprisingly., it generates the data in about a minute,. and then the device inserts the data into its sqlce db.. so, im wondering if there is a way to turn off buffering on the webserice call and populate the database as data comes back... rather than., send command-> wait for entire dataset -> insert data set...... i want to do a send command -> wait a bit, ->do a bit -> loop a bit
sadly i have to communicate thru a web service... client needs top security and will not allow access to db's directly from desktop/work station... all db's must be accessed thru web serivce...
The BackgroundWorker class hasn't changed at all (as far as I'm aware) from .NET 2.0 to 3.5.
My knowledge of Web Services is not extensive but, as far as I'm aware, calling a web method is essentially the same as calling a local method. If your method returns a DataSet then you can't really do anything until that method returns because you have no data until then. If you can somehow access the data piecemeal then you can certainly use it as you receive it. That's beyond the scope of this thread though. I'd suggest you post a question relating to this specifically on the appropriate forum.
-
May 28th, 2008, 08:13 AM
#10
Re: Using the BackgroundWorker Component
Originally Posted by zxed
question... this was posted in 2007.. im guessing it has not really for .net 3.5... is it?
The 3.5 framework literally sits on top of the 3.0 framework and the 3.0 framework literally sits on top of the 2.0 framework which means unless there's a new backgroundworker in the 3.5 framework (which you would know about it if you were using it because you would have to explicitly reference it) the only background worker is the 2.0 one.
Another interesting tip: the 3.0 and 3.5 frameworks don't have a CLR, it's the 2.0 framework that has the CLR we all use and love.
-
May 28th, 2008, 11:27 AM
#11
Lively Member
Re: Using the BackgroundWorker Component
thanks for getting back to me., i will figure out the web service thingy, and create a sep thread with code + examples.
-
Jun 5th, 2008, 11:52 AM
#12
Fanatic Member
Re: Using the BackgroundWorker Component
I have a question. When a large number of files are being copied, how can I accurately show overall process progressbar feedback ? Update the form's progressbar from the backgroundworker...
Thanks for any help.
-
Jun 5th, 2008, 06:41 PM
#13
Re: Using the BackgroundWorker Component
Originally Posted by Xancholy
I have a question. When a large number of files are being copied, how can I accurately show overall process progressbar feedback ? Update the form's progressbar from the backgroundworker...
Thanks for any help.
This thread already shows you how to update a ProgressBar to indicate the progress of a background operation so I'm not going to go over that. Your issue is determining what constitutes 100% and determining what your current percentage is.
Now, the first thing to note is that progress does NOT have to represent a percentage. You can, if you want, set the Maximum of your ProgressBar to 100 and then set the Value to a percentage. Alternatively you could set the Maximum to the number files you need to process and then increment the Value by 1 each time a file is processed. Another alternative would be to set the Maximum to the total size of all the files to be processed and then increment the Value by the size of the file each time one is processed.
-
Jun 5th, 2008, 08:14 PM
#14
Fanatic Member
Re: Using the BackgroundWorker Component
Last edited by Xancholy; Jun 5th, 2008 at 08:52 PM.
-
Jun 5th, 2008, 08:19 PM
#15
Re: Using the BackgroundWorker Component
Originally Posted by Xancholy
The other option would be to measure total of filesizes being copied and progressbar it in a linear way, yes ?
The only time you can update the ProgressBar is after each file has been processed because copying a file is a synchronous operation. You can't provide any feedback during the copy operation.
-
Jun 10th, 2008, 10:05 AM
#16
Addicted Member
Re: Using the BackgroundWorker Component
Last edited by doran_doran; Jun 10th, 2008 at 10:38 AM.
-
Jun 10th, 2008, 10:15 AM
#17
Re: Using the BackgroundWorker Component
doran, you're going to have to tidy that post up before I'll even consider looking at it.
-
Jun 30th, 2008, 11:33 PM
#18
Addicted Member
Re: Using the BackgroundWorker Component
I found that I didn't even need the Sleep from the Threading. The worker seems to use just the right amount of resources to get the task done without crashing or overloading.
-
Jul 22nd, 2008, 08:43 PM
#19
Re: Using the BackgroundWorker Component
Here is a new example based on the code from the first post. Follow the same instructions as before but this time also set the WorkerSupportsCancellation property of the BackgroundWorker to True and add a Button to the form. Now add this code to your form:
vb.net Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Raise the DoWork event in a worker thread. Me.BackgroundWorker1.RunWorkerAsync() End Sub 'This method is executed in a worker thread. Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _ ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker) For i As Integer = 1 To 100 If worker.CancellationPending Then 'The user has cancelled the background operation. e.Cancel = True Exit For End If 'Raise the ProgressChanged event in the UI thread. worker.ReportProgress(i, i & " iterations complete") 'Perform some time-consuming operation here. Threading.Thread.Sleep(250) Next i End Sub 'This method is executed in the UI thread. Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _ ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged Me.ProgressBar1.Value = e.ProgressPercentage Me.Label1.Text = TryCast(e.UserState, String) End Sub 'This method is executed in the UI thread. Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _ ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted If e.Cancelled Then 'The background operation was cancelled. Me.Label1.Text = "Operation cancelled" Else 'The background operation completed normally. Me.Label1.Text = "Operation complete" End If End Sub Private Sub Button1_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles Button1.Click 'Only cancel the background opertion if there is a background operation in progress. If Me.BackgroundWorker1.IsBusy Then Me.BackgroundWorker1.CancelAsync() End If End Sub
If you run the project and let it go then the ProgressBar will fill and eventually the Label will report that the operation completed. If, however, you click the Button at some point, the Progressbar will halt and the Label will report that the operation was cancelled.
-
Jul 23rd, 2008, 06:39 AM
#20
Fanatic Member
Re: Using the BackgroundWorker Component
-
Oct 21st, 2008, 06:17 AM
#21
Junior Member
Re: Using the BackgroundWorker Component
Originally Posted by jmcilhinney
Create a new Windows Forms project. Add a Label, a ProgressBar and a BackgroundWorker to the form. Set the BackgroundWorker's WorkerReportsProgress property to True. Add the following code then run the project.
VB.NET Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Raise the DoWork event in a worker thread.
Me.BackgroundWorker1.RunWorkerAsync()
End Sub
'This method is executed in a worker thread.
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
For i As Integer = 1 To 100
'Raise the ProgressChanged event in the UI thread.
worker.ReportProgress(i, i & " iterations complete")
'Perform some time-consuming operation here.
Threading.Thread.Sleep(250)
Next i
End Sub
'This method is executed in the UI thread.
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
Me.Label1.Text = TryCast(e.UserState, String)
End Sub
'This method is executed in the UI thread.
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Me.Label1.Text = "Operation complete"
End Sub
hi, i used the above code .. and i got this error:
Cross-thread operation not valid: Control 'Label1' accessed from a thread other than the thread it was created on.
need your advise...
-
Oct 21st, 2008, 05:41 PM
#22
Re: Using the BackgroundWorker Component
Originally Posted by numbskull77
hi, i used the above code .. and i got this error:
Cross-thread operation not valid: Control 'Label1' accessed from a thread other than the thread it was created on.
need your advise...
You certainly didn't use just that code. If you go back to post #4 I specifically demonstrate how to access controls using a BackgroundWorker, i.e. either from the ProgressChanged event handler or the RunWorkerCompleted event handler. That's because they are both executed in the UI thread. You cannot access controls from the DoWork event handler, as you must be, because it is executed in a background thread.
If you can't use those two aforementioned events for some reason then you will need to use explicit delegation. Follow the Controls & Multi-threading link in my signature for an explanation of why and how.
-
Oct 31st, 2008, 07:53 PM
#23
Fanatic Member
Re: Using the BackgroundWorker Component
Hi John ,
i have a problem with stopping the BackgroundWorker
i have 2 button start & stop
so basically the start button download some data using webclient
Code:
bgwkdata.RunWorkerAsync()
and for stop button i have
Code:
bgwkdata.CancelAsync()
so the problem is the stop button not doing the job the BackgroundWorker is still running
btw i have set option true to Workersupportcancellation and also
Code:
If bgwkdata.CancellationPending Then
e.Cancel = True
Exit Sub
End If
Thanks
Last edited by killer7k; Oct 31st, 2008 at 08:17 PM.
-
Oct 31st, 2008, 09:22 PM
#24
Re: Using the BackgroundWorker Component
Originally Posted by killer7k
Hi John ,
i have a problem with stopping the BackgroundWorker
i have 2 button start & stop
so basically the start button download some data using webclient
Code:
bgwkdata.RunWorkerAsync()
and for stop button i have
Code:
bgwkdata.CancelAsync()
so the problem is the stop button not doing the job the BackgroundWorker is still running
btw i have set option true to Workersupportcancellation and also
Code:
If bgwkdata.CancellationPending Then
e.Cancel = True
Exit Sub
End If
Thanks
Presumably that code is never being hit during your time-consuming operation. For instance, there's not much point doing this:
vb.net Code:
If myBGW.CancellationPending Then
e.Cancel = True
Exit Sub
End If
For count As Integer = 1 To 100
'Do something.
Next
If myBGW.CancellationPending Then
e.Cancel = True
Exit Sub
End If
because you don't ever check whether the operation needs to be cancelled anywhere in the loop. You'd need to check inside the loop. If you don't have a loop then you'll just have to check in between the steps of your operation. You can only check whether the operation needs to be cancelled while the background thread is not busy doing something else. If your background thread simply makes one synchronous method call then you're out of luck. It simply can't be cancelled. There need to be breaks somewhere in your code that you can insert those checks.
-
Nov 1st, 2008, 09:36 AM
#25
Fanatic Member
Re: Using the BackgroundWorker Component
Hi John ,
here you what i have done
Code:
Private Sub bgwkdata_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwkdata.DoWork
If bgwkdata.CancellationPending Then
e.Cancel = True
Exit Sub
End If
//my boucle here
If bgwkdata.CancellationPending Then
e.Cancel = True
Exit Sub
End If
and on btnstop :
Code:
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
bgwkdata.CancelAsync()
End Sub
Edit : i get it to work i placed the code in the boucle
Thanks
Last edited by killer7k; Nov 1st, 2008 at 11:53 AM.
-
Apr 7th, 2009, 10:24 AM
#26
Hyperactive Member
Re: Using the BackgroundWorker Component
hi jmc
can we use background worker on smart device application ?
i am developing a app. in vb.net 2008 for a smart device.
i am trying to locate the background worker that u mentioned to be added to the form but am not able to find it.
do i have to import something to get that option or what ?
pls. guide
thankx a lot
-
Apr 7th, 2009, 12:00 PM
#27
Re: Using the BackgroundWorker Component
Originally Posted by kuldevbhasin
hi jmc
can we use background worker on smart device application ?
i am developing a app. in vb.net 2008 for a smart device.
i am trying to locate the background worker that u mentioned to be added to the form but am not able to find it.
do i have to import something to get that option or what ?
pls. guide
thankx a lot
The location of the BW is this: System.ComponentModel.BackgroundWorker
So I would start by looking for it in the System.ComponentModel namespace
-
Apr 7th, 2009, 07:15 PM
#28
Re: Using the BackgroundWorker Component
Originally Posted by kuldevbhasin
hi jmc
can we use background worker on smart device application ?
i am developing a app. in vb.net 2008 for a smart device.
i am trying to locate the background worker that u mentioned to be added to the form but am not able to find it.
do i have to import something to get that option or what ?
pls. guide
thankx a lot
The CF contains less than the full Framework and the BackgroundWorker class is simply not necessary. It's convenient but it doesn't do anything you can't do yourself with the Thread class and delegates.
-
Apr 8th, 2009, 04:59 AM
#29
Hyperactive Member
Re: Using the BackgroundWorker Component
thankx for ur reply
i have developed a prog. for bills generation on a smart device. the smart device is using GPRS and web service to connect to the data stored on the server.
what i was thinking was that if GPRS is not available then the prog. should generate and store the data on the device itself as sqlce is available on the device. now as soon as the GPRS is available i was thinking of sending the data to the web server in the background.
for this reason was trying for background worker. so that while the bills get generated the local bills r also getting posted simultaniously in the background.
am i right in this thinking or is there any other way.
pls. guide.
thankx
-
Apr 8th, 2009, 05:11 AM
#30
Re: Using the BackgroundWorker Component
Originally Posted by kuldevbhasin
thankx for ur reply
i have developed a prog. for bills generation on a smart device. the smart device is using GPRS and web service to connect to the data stored on the server.
what i was thinking was that if GPRS is not available then the prog. should generate and store the data on the device itself as sqlce is available on the device. now as soon as the GPRS is available i was thinking of sending the data to the web server in the background.
for this reason was trying for background worker. so that while the bills get generated the local bills r also getting posted simultaniously in the background.
am i right in this thinking or is there any other way.
pls. guide.
thankx
That is not the topic of this thread. You should ask such questions in new threads in the appropriate forum.
-
Apr 9th, 2009, 08:10 PM
#31
Hyperactive Member
Re: Using the BackgroundWorker Component
hey jim
why do you declare a new worker:-
vb Code:
Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
you already have one
i couldn't understand it
and why you didn't dispose the backgroundworker?
-
Apr 9th, 2009, 08:37 PM
#32
Re: Using the BackgroundWorker Component
Originally Posted by snakeman
hey jim
why do you declare a new worker:-
vb Code:
Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
you already have one
i couldn't understand it
and why you didn't dispose the backgroundworker?
There's a difference between "declaring" and "creating". That code declares a BackgroundWorker variable but it does NOT create a BackgroundWorker object. It simply takes the 'sender' parameter, which is the object that raised the event, and casts it as type BackgroundWorker. By assigning that to the local variable I am then able to access members of the BackgroundWorker type, which I couldn't do on 'sender' because it is type Object.
Yes, I could have simply used the existing member variable, i.e. Me.BackgroundWorker1, and had the same result. The reason that casting the 'sender' is useful is that it allows you to handle events for multiple BackgroundWorkers with the same method. If you only have one BackgroundWorker then it's not required, although it is considered best practice.
-
Apr 9th, 2009, 09:23 PM
#33
Hyperactive Member
Re: Using the BackgroundWorker Component
-
Jun 10th, 2009, 03:21 AM
#34
New Member
Re: Using the BackgroundWorker Component
Cant seem to figure out what Im doing wrong here, I get a type expected error from vb when I try to compile from the first backgroundWorker part of this (it automatically makes it a lowercase "b" could that have something to do with this?)
Code:
Dim worker As backgroundWorker = DirectCast(sender, backgroundWorker)
entire code
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Raise the DoWork event in a worker thread.
Me.BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.BackgroundWorker1.RunWorkerAsync(CInt(Me.NumericUpDown1.Value))
End Sub
'This method is executed in a worker thread.
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim worker As backgroundWorker = DirectCast(sender, backgroundWorker)
Dim iterationCount As Integer = 0
If TypeOf e.Argument Is Integer Then
iterationCount = CInt(e.Argument)
End If
For i As Integer = 1 To iterationCount Step 1
'Do something here.
Next i
End Sub
'This method is executed in the UI thread.
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
Me.Label1.Text = TryCast(e.UserState, String)
End Sub
'This method is executed in the UI thread.
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Me.Label1.Text = "Operation complete"
End Sub
End Class
Thanks in advance for any help!
-
Jun 10th, 2009, 04:22 AM
#35
Re: Using the BackgroundWorker Component
Originally Posted by Tommi
Cant seem to figure out what Im doing wrong here, I get a type expected error from vb when I try to compile from the first backgroundWorker part of this (it automatically makes it a lowercase "b" could that have something to do with this?)
Code:
Dim worker As backgroundWorker = DirectCast(sender, backgroundWorker)
entire code
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Raise the DoWork event in a worker thread.
Me.BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.BackgroundWorker1.RunWorkerAsync(CInt(Me.NumericUpDown1.Value))
End Sub
'This method is executed in a worker thread.
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim worker As backgroundWorker = DirectCast(sender, backgroundWorker)
Dim iterationCount As Integer = 0
If TypeOf e.Argument Is Integer Then
iterationCount = CInt(e.Argument)
End If
For i As Integer = 1 To iterationCount Step 1
'Do something here.
Next i
End Sub
'This method is executed in the UI thread.
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
Me.Label1.Text = TryCast(e.UserState, String)
End Sub
'This method is executed in the UI thread.
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Me.Label1.Text = "Operation complete"
End Sub
End Class
Thanks in advance for any help!
The BackgroundWorker class is a member of the System.ComponentModel namespace. If you haven't imported that namespace then you'll need to qualify the class name with the namespace, as for all the 'e' parameters in the event handlers.
-
Jun 19th, 2009, 07:23 AM
#36
New Member
Re: Using the BackgroundWorker Component
Originally Posted by jmcilhinney
The BackgroundWorker class is a member of the System.ComponentModel namespace. If you haven't imported that namespace then you'll need to qualify the class name with the namespace, as for all the 'e' parameters in the event handlers.
It's working now, thanks for the help!
-
Oct 5th, 2009, 09:32 AM
#37
Fanatic Member
Re: Using the BackgroundWorker Component
Excellent post! It has helped me a lot!! Keep up the always great work jmcilhinney!
-
Mar 29th, 2010, 02:49 PM
#38
Hyperactive Member
Re: Using the BackgroundWorker Component
I have tried to adapt your code to a console application and it seems to working as far as running the background worker, and I do have the ProgressChanged and RunWorkerCompleted functions to monitor it, the process ends after one cycle of its do work process.
Code:
While Not response.Equals("3")
Try
Console.Write("Enter choice: ")
response = Console.ReadLine()
Console.WriteLine()
If response.Equals("1") Then
Console.WriteLine("Thread 1 doing work" & vbNewLine)
engine.LoadFile()
'tm.SetApartmentState(ApartmentState.STA)
'tm.Start()
response = String.Empty
ElseIf response.Equals("2") Then
Console.WriteLine("Starting a second Thread")
'ts.Start()
report()
response = String.Empty
End If
'ts.Join()
'tm.Join()
Catch ex As Exception
as you can see the commented parts was an attempt to try threads; wasnt successful.
Here is where the background worker is being called from, sub engine.loadfile()
Code:
bgw.WorkerReportsProgress = True
AddHandler bgw.DoWork, New DoWorkEventHandler(AddressOf Search)
bgw.RunWorkerAsync()
AddHandler bgw.ProgressChanged, New ProgressChangedEventHandler(AddressOf ProgressChanged)
AddHandler bgw.RunWorkerCompleted, New RunWorkerCompletedEventHandler(AddressOf RunWorkerComplete)
the actual do work process
Code:
Private Sub Search(ByVal sender As Object, ByVal e As DoWorkEventArgs)
...
BloObj.Main(connectionString, userAgent, fileName, clientName, clientProj)
Dim i As Integer
For i = 1 To 100
CType(sender, BackgroundWorker).ReportProgress(i)
Thread.Sleep(100)
Next
Console.ResetColor()
Console.Write("Search Complete, Do you wish to generate queries, y or n? ")
reply = Console.ReadLine()
If reply = "y" Then
report()
ElseIf reply = "n" Then
Console.Clear()
main()
End If
Catch ex As Exception
errormessage = ex.Message
End Try
Any ideas appreciated.
-- Please rate me if I am helpful --
-
Mar 29th, 2010, 07:10 PM
#39
Re: Using the BackgroundWorker Component
Originally Posted by cengineer
I have tried to adapt your code to a console application and it seems to working as far as running the background worker, and I do have the ProgressChanged and RunWorkerCompleted functions to monitor it, the process ends after one cycle of its do work process.
Code:
While Not response.Equals("3")
Try
Console.Write("Enter choice: ")
response = Console.ReadLine()
Console.WriteLine()
If response.Equals("1") Then
Console.WriteLine("Thread 1 doing work" & vbNewLine)
engine.LoadFile()
'tm.SetApartmentState(ApartmentState.STA)
'tm.Start()
response = String.Empty
ElseIf response.Equals("2") Then
Console.WriteLine("Starting a second Thread")
'ts.Start()
report()
response = String.Empty
End If
'ts.Join()
'tm.Join()
Catch ex As Exception
as you can see the commented parts was an attempt to try threads; wasnt successful.
Here is where the background worker is being called from, sub engine.loadfile()
Code:
bgw.WorkerReportsProgress = True
AddHandler bgw.DoWork, New DoWorkEventHandler(AddressOf Search)
bgw.RunWorkerAsync()
AddHandler bgw.ProgressChanged, New ProgressChangedEventHandler(AddressOf ProgressChanged)
AddHandler bgw.RunWorkerCompleted, New RunWorkerCompletedEventHandler(AddressOf RunWorkerComplete)
the actual do work process
Code:
Private Sub Search(ByVal sender As Object, ByVal e As DoWorkEventArgs)
...
BloObj.Main(connectionString, userAgent, fileName, clientName, clientProj)
Dim i As Integer
For i = 1 To 100
CType(sender, BackgroundWorker).ReportProgress(i)
Thread.Sleep(100)
Next
Console.ResetColor()
Console.Write("Search Complete, Do you wish to generate queries, y or n? ")
reply = Console.ReadLine()
If reply = "y" Then
report()
ElseIf reply = "n" Then
Console.Clear()
main()
End If
Catch ex As Exception
errormessage = ex.Message
End Try
Any ideas appreciated.
Your code doesn't really make sense to me. I don't see why you have a BGW at all. You seem to be prompting the user for data from the background thread. Usually the point of background threads is to perform time-consuming processing in the background so that the user can still interact with the application in the foreground. What's the point of having multiple threads in your case?
-
Mar 30th, 2010, 08:44 AM
#40
Hyperactive Member
Re: Using the BackgroundWorker Component
Sorry about the confusion... I need to implement threads on the menu options I have and the one process is a long search crawling process which after it starts does not involve any user interaction.
I figured the background process would work well here.
Code:
Console.ResetColor()
Console.Write("Begin Search -- Discovery Search, y or n? ")
response1 = Console.ReadLine()
If response1 = "y" Then
Console.ForegroundColor = ConsoleColor.White
Console.WriteLine()
Console.Write("Enter client name: ")
clientName = Console.ReadLine()
Console.WriteLine()
Console.Write("Enter client project: ")
clientProj = Console.ReadLine()
' This is where I would like to call the background process
bgw.WorkerReportsProgress = True
AddHandler bgw.DoWork, New DoWorkEventHandler(AddressOf Search)
bgw.RunWorkerAsync()
AddHandler bgw.ProgressChanged, New ProgressChangedEventHandler(AddressOf ProgressChanged)
AddHandler bgw.RunWorkerCompleted, New RunWorkerCompletedEventHandler(AddressOf RunWorkerComplete)
'bgw.ReportProgress(80)
' I was unable to make sure the process returned to the menu options so I put in a return here to the main where its called from
Return
'Search()
The actual search process only contains a user response after it supposed to complete. The background process is not only one function but involves calling other classes and functions to the crawling process. As i mentioned before I tried using threads but found that it did not run in the background as I intended. Also the background process is stopping before completion without any indication. I placed breakpoints to catch the possible errors but the app just ends after a while.
-- Please rate me if I am helpful --
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
|