Question How to access labels through backgroundworker_DoWork?

skyfe

Member
Joined
Jan 16, 2009
Messages
14
Programming Experience
3-5
Hi,

Was wondering: How can I set for example a value of a label within the progress that my bagroundworker does? So for example my backgroundworker is scanning files (in my case) and I want it to set label1.text equal to the name of the file it's scanning at that moment, but how do I do this? because when I do like:

Private Sub bgwScan1_DoWork(ByVal sender As Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwScan1.DoWork

[... some code ...]

label2.text = foundDir

[... some code ...]

End Sub

Then it says "access denied" or something. So how do I change the label2.text with my backgroundworker progress?

Thanks in advanced,

Skyfe.
 
Set the BackgroundWorker's ReportProgress to True in the designer.

In the DoWork event

VB.NET:
Me.BackgroundWorker1.ReportProgress(0, foundDir)

In the ProgressChanged event

VB.NET:
Me.label2.Text = e.UserState
 
Thanks a lot! It works! :D

Thanks so much, will be using this, and I guess I can use 2 backgroundworkers at same time, right?
 
Ok thanks, well just found a way to do it with only 1 backgroundworker, while using 2 progressbars and 2 subs ^^ With using the e.UserState variable, reporting the progresses into their and when progresspercentage = 0, then the e.UserState would give the progress of the 2nd progressbar instead of the first, and update that one, etc., and so worked:D
Well, Thanks for the help, got it all working now :)
 
Back
Top