Backgroundworker with public function and listbox
Hi,
I'm struggling with a thing or two...and i've read some stuff about the backgroundworker but im stuck :o
Basically what i have is a :
PUBLIC FUNCTION TEST (path as string) : which at the end of work , adds an item to the form1.listbox. That works quite nice but freezes the app.
I've tried using the backgroundworker, the function in the background worker seems to work nicely, the bad thing is that i dont get any results in the form1.listbox.
Any ideas on how to make the listbox add items?? :confused:
Cheers! :wave:
Re: Backgroundworker with public function and listbox
No idea what your code looks like.
I'd suggest starting here: http://vbnotebookfor.net/2007/09/24/...rker-in-vbnet/
Re: Backgroundworker with public function and listbox
You probably need to use a delegate sub/function in order to update the form's control on the fly.
So:
vb Code:
private delegate sub Del_AddListBoxItem(value as string)
Then in your DoWork method or whatever method the background worker performs you would have something like:
vb Code:
Public Function (path as string) As Boolean 'don't know what you want to return.
// work done
Dim d as new Del_AddListBoxItem(AddressOf formAddListBoxItem)
Me.Invoke(d, new object(){"testItem"})
End Function
You'll have to have a Public or Private Sub called 'myAddListBoxItem'.
Would look something like:
vb Code:
Private Sub myAddListBoxItem(value as string)
Me.ListBox1.Items.Add(value)
End Sub
Basically this is required when you are trying to update a GUI control from a thread other than the one
the form was created on.
Justin
Re: Backgroundworker with public function and listbox
i'll check it out ..
well basically...my public function does calculations and adds results to the main form's listbox.
so , when i add that function in the backgroundworker_dowork sub, the calculations withing the public function are being made whilte the result does not get printed out on the main form listbox.
Re: Backgroundworker with public function and listbox
Declare a delegate sub/function with the same parameter signature like in my previous post.
Create a public/private sub/function with the same declaration as the delegate like in my previous post.
Instead of calling ListBox1.Items.Add(value) directly in your backgroundworker_dowork sub, use the form's Invoke method like in my previous post.
See if that gets the listbox populated.
Justin
Re: Backgroundworker with public function and listbox
First of all, thank you very much for your help!
This is the error i get :
Error on: Form1.lstScreens.Invoke(d, New Object, {"testItem"})
Reason : Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
This is what i did:
Code:
Public Delegate Sub Del_AddListBoxItem(ByVal value As String)
Code:
Public Function Test() as String
// WORK DONE
Dim d As New Del_AddListBoxItem(AddressOf myAddListBoxItem)
Form1.lstScreens.Invoke(d, New Object, {"testItem"})
End Function
Code:
Public Sub myAddListBoxItem(ByVal value As String)
Form1.listbox1.Items.Add(value)
End Sub
Re: Backgroundworker with public function and listbox
Form1.lstScreens.Invoke(d, New Object, {"testItem"})
should be
Form1.Invoke(d, New Object(){"TheValueYouWantToAdd"})
or
Me.Invoke(d, New Object(){"TheValueYouWantToAdd"})
Me, referring to the form the backgroundworker has been dropped on.
Are you starting the Worker in the form_load?
Justin
Re: Backgroundworker with public function and listbox
Quote:
Originally Posted by
MonkOFox
Declare a delegate sub/function with the same parameter signature like in my previous post.
Create a public/private sub/function with the same declaration as the delegate like in my previous post.
Instead of calling ListBox1.Items.Add(value) directly in your backgroundworker_dowork sub, use the form's Invoke method like in my previous post.
See if that gets the listbox populated.
Justin
The whole point of the BackgroundWorker is so you don't have to manually do this.
YourBackgroundWorker.ReportProgress lets you send ProgressPercentage and UserState to the YourBackgrounWorker_ProgressChanged event. From there you can update any controls on the UI.
The link I posted goes through all of this step by step.
Re: Backgroundworker with public function and listbox
Quote:
Originally Posted by
MonkOFox
You probably need to use a delegate sub/function in order to update the form's control on the fly.
So:
vb Code:
private delegate sub Del_AddListBoxItem(value as string)
Then in your DoWork method or whatever method the background worker performs you would have something like:
vb Code:
Public Function (path as string) As Boolean 'don't know what you want to return.
// work done
Dim d as new Del_AddListBoxItem(AddressOf formAddListBoxItem)
Me.Invoke(d, new object(){"testItem"})
End Function
You'll have to have a Public or Private Sub called 'myAddListBoxItem'.
Would look something like:
vb Code:
Private Sub myAddListBoxItem(value as string)
Me.ListBox1.Items.Add(value)
End Sub
Basically this is required when you are trying to update a GUI control from a thread other than the one
the form was created on.
Justin
I did as you say now...im not getting anymore errors but the listbox is not getting updated :sick:
Hmm...any ideas? As you asked....i'm running .RunWorkerAsync() in a button_click sub
I'm calling my public procedure in the .DoWork procedure od backgroundworker ...
Quote:
Originally Posted by
MattP
I'll try to implement that example on my example, thanks.
Re: Backgroundworker with public function and listbox
Quote:
YourBackgroundWorker.ReportProgress lets you send ProgressPercentage and UserState to the YourBackgrounWorker_ProgressChanged event. From there you can update any controls on the UI.
Ohhh, ok. I didn't know that. I'm used to using threads and I thought the idea behind updating UI controls would be the same.
Thanks for the clarification,
Justin
Re: Backgroundworker with public function and listbox
It is very much the same. You can certainly raise events from any thread, but those events are on the thread that raised them. That's not so useful, since you need the UI thread to handle any control interactions. Therefore, you need the events to be raised on the UI thread. You can do that from a standard thread using .Post on the SynchronizationContext, but one of the reasons the BackGroundWorker was added was to make this easier to do. It raises the ProgressChanged (and one other) on the UI thread automatically, without needing to deal with invoking or SynchronizationContexts.
It's good to know about the other stuff, but the BGW does simplify things by hiding that other stuff.
Re: Backgroundworker with public function and listbox
Yeah but if the background workerhas a built in event, it would make sense to use that. But I learnedsomething new : ). Is there an android app for this site?
Re: Backgroundworker with public function and listbox
Well, im able to do a simple background work as. All my previous attempts have failed, but your help is greatly appreciated!
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TestWorker.RunWorkerAsync()
End Sub
Private Sub TestWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles TestWorker.DoWork
Dim i As Integer
For i = 0 To 20
TestWorker.ReportProgress(i)
Next i
End Sub
Private Sub TestWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles TestWorker.ProgressChanged
TextBox1.Text = e.ToString
End Sub
But in my public function where i have a, i add :
Code:
for i...
try
myresult = "result string"
form1.TestWorker.ReportProgress(i)
catch
end try
next
nothing happends :(
Re: Backgroundworker with public function and listbox
form1 is almost certainly wrong. What that will do is use the default instance of the form. I have ranted on about the evils of the default instance so many times I don't feel like going into it again. By using form1, in this case, you are doing that action on a very specific instance of the form, which may not even be the instance that is visible, and it may not be the instance where the code is running.
Try using Me rather than form1.
Re: Backgroundworker with public function and listbox
Have a look at the msdn documentation: http://msdn.microsoft.com/en-us/libr...undworker.aspx. There are a couple of example showing it in use. Pay attention to where ReportProgress is being called and how they do it.