-
Mar 17th, 2014, 09:23 AM
#1
Thread Starter
Hyperactive Member
background worker progress changed
Hi,
I'm having trouble getting the string from progress changed in bgw.
Well, when i type the following code it works:
HTML Code:
msgbox(e.userstate)
In this case i get the progress changed (string value)
But if i write in progresschanged :
HTML Code:
text1.text=e.userstate.tostring
i get nothing...
this is how i send data to reportprogress:
HTML Code:
WorkerL.ReportProgress(10, "string")
Any ideas?
Cheers!
Thanks for helping me out.
-
Mar 20th, 2014, 05:37 AM
#2
Thread Starter
Hyperactive Member
Re: background worker progress changed
bump?
Thanks for helping me out.
-
Mar 20th, 2014, 06:02 AM
#3
Re: background worker progress changed
I created this simple test and it works as advertised.
Code:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
BackgroundWorker1.ReportProgress(10, "string")
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
TextBox1.Text = DirectCast(e.UserState, String)
End Sub
The UserState is an object.
-
Mar 20th, 2014, 06:10 AM
#4
Addicted Member
Re: background worker progress changed
Is you code ever calling the WorkerL.ReportProgress(10, "string") portion of your code? Set a breakpoint on that line and see if it ever hits it.
You might need to provide more details of how your code works. From what you posted it looks like it should work but obviously you are missing something if its not, but i cant tell from what information you provided.
-
Mar 20th, 2014, 01:51 PM
#5
Thread Starter
Hyperactive Member
Re: background worker progress changed
Thanks for the reply...
This is what i do:
start worker and calling a function in _DoWork ... that same function reports progress to the worker...which gets the reported progress and shows it in a message box but it wont show it in text box or any other form :/
So, if i put in ProgressChanged something like :
i will get the the reported string ....
otherwise it wont work...i really don't know why
Thanks for helping me out.
-
Mar 20th, 2014, 02:44 PM
#6
Re: background worker progress changed
Originally Posted by batori
Thanks for the reply...
This is what i do:
start worker and calling a function in _DoWork ... that same function reports progress to the worker...which gets the reported progress and shows it in a message box but it wont show it in text box or any other form :/
So, if i put in ProgressChanged something like :
i will get the the reported string ....
otherwise it wont work...i really don't know why
What? Dowork calls a function that reports progress? I am confused.
-
Mar 20th, 2014, 03:05 PM
#7
Thread Starter
Hyperactive Member
Re: background worker progress changed
Ok, let me explain in a better way.
So,
Code:
Private Sub WorkerL_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles WorkerL.DoWork
CALCULATE_NUMBERS(selected_numbers)
End Sub
Code:
Private Sub WorkerL_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles WorkerL.ProgressChanged
' this code works, shows the state (string)
MsgBox(e.UserState)
'this code below returns nothing
txtDebug.Text = DirectCast(e.UserState, String)
End Sub
Code:
Public Function CALCULATE_NUMBERS(num as integer)
' Do some work
frmMain.WorkerL.ReportProgress(10, "string")
End Fucntion
Thanks for helping me out.
-
Mar 20th, 2014, 04:20 PM
#8
Re: background worker progress changed
Have you actually set WorkerReportsProgress to true?
-
Mar 20th, 2014, 04:22 PM
#9
Re: background worker progress changed
You do realize your not actually reporting any progress.
Using the BackgroundWorker Component
-
Mar 20th, 2014, 05:35 PM
#10
Thread Starter
Hyperactive Member
Re: background worker progress changed
Yes, of course it is.
I'm even getting a message box when progress is "triggered" ... it's just that i cant get that userstate to a textbox or label...i can only get it in a message box form...isn't that strange??
Thanks for helping me out.
-
Mar 20th, 2014, 06:36 PM
#11
Re: background worker progress changed
I don't think we are seeing the whole picture here, but just for grins as giggles, try replacing the code in the progress handler with this:
Code:
If InvokeRequired Then
MsgBox("Progress from wrong thread")
Me.Invoke(New Action(Of Object, System.ComponentModel.ProgressChangedEventArgs)(AddressOf WorkerL_ProgressChanged), New Object() {sender, e})
Else
Text1.Text = e.UserState.ToString
End If
and let us know what happens.
-
Mar 20th, 2014, 06:44 PM
#12
Re: background worker progress changed
Originally Posted by batori
Ok, let me explain in a better way.
So,
Code:
Private Sub WorkerL_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles WorkerL.DoWork
CALCULATE_NUMBERS(selected_numbers)
End Sub
Code:
Private Sub WorkerL_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles WorkerL.ProgressChanged
' this code works, shows the state (string)
MsgBox(e.UserState)
'this code below returns nothing
txtDebug.Text = DirectCast(e.UserState, String)
End Sub
Code:
Public Function CALCULATE_NUMBERS(num as integer)
' Do some work
frmMain.WorkerL.ReportProgress(10, "string")
End Fucntion
When I run this
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
why()
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
TextBox1.Text = DirectCast(e.UserState, String)
End Sub
Private Sub why()
BackgroundWorker1.ReportProgress(10, "string")
End Sub
The "string" appears in the textbox. Try removing frmMain from this frmMain.WorkerL.ReportProgress(10, "string")
-
Mar 21st, 2014, 02:30 AM
#13
Thread Starter
Hyperactive Member
Re: background worker progress changed
I tried TnTinMN method , it did not show anything....
I tried dbasnett method , and it worked....
So , what i did, i moved the Function from being Public in the module, to being Public in frmMain and then.....it works!
But then again...when i had the function in the module, the progress did "arrive" to frmmain, and it was shown in the msgbox but not in any other textbox. label or whatsoever....but having a public function , in the frmMain code...worked.
But i still don't get it...
Thanks for helping me out.
-
Mar 21st, 2014, 10:02 AM
#14
Addicted Member
Re: background worker progress changed
Originally Posted by batori
I tried TnTinMN method , it did not show anything....
I tried dbasnett method , and it worked....
So , what i did, i moved the Function from being Public in the module, to being Public in frmMain and then.....it works!
But then again...when i had the function in the module, the progress did "arrive" to frmmain, and it was shown in the msgbox but not in any other textbox. label or whatsoever....but having a public function , in the frmMain code...worked.
But i still don't get it...
The reason is that a MODULE has global scope and simply calling frmMain is referencing the TYPE of form frmMain and not the instantiated form. I suppose if you prefixed frmMain with My.Forms.frmMain then it would work in the module but you did the right thing by moving your function to the form.
Last edited by OICU812; Mar 21st, 2014 at 10:07 AM.
-
Mar 21st, 2014, 12:10 PM
#15
Thread Starter
Hyperactive Member
Re: background worker progress changed
I tried your suggestion to use my.forms.frmMain...still does not work ...
i can only get the string in progresschanged by using MSGBOX .... as usual....
i still dont get it ....
Thanks for helping me out.
-
Mar 21st, 2014, 12:14 PM
#16
Re: background worker progress changed
Did you try
WorkerL.ReportProgress(10, "string")
-
Mar 21st, 2014, 12:19 PM
#17
Thread Starter
Hyperactive Member
Re: background worker progress changed
yes, with both my.forms.frmmain.workerl and with frmmain.workerL...
progresschanged is triggered and msgbox return the value...but nothing else does.... ???? :/
Thanks for helping me out.
-
Mar 21st, 2014, 01:05 PM
#18
Re: background worker progress changed
So out of curiosity I tried this
Code:
Public Class Form1
Public WithEvents BackgroundWorker1 As New System.ComponentModel.BackgroundWorker
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Do While True
why()
Threading.Thread.Sleep(1000)
Loop
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
TextBox1.Text = DirectCast(e.UserState, String) & " " & e.ProgressPercentage.ToString
End Sub
Private Sub why()
BackgroundWorker1.ReportProgress(10, DateTime.Now.ToString("HH:mm:ss.f"))
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
test()
End Sub
End Class
Module Module1
Public Sub test()
Form1.BackgroundWorker1.ReportProgress(20, "TEST")
End Sub
End Module
Works, but I personally don't like using the default instance.
-
Mar 21st, 2014, 01:20 PM
#19
Re: background worker progress changed
Ok... hold on... I'm completely confused... if the process that's doing the work is in a module, that should be fine... the UI should only be updated in the ReportProgress event, which should be back on the main form... so... there shouldn't be any need to access the instance let alone the default instance...
I'm beyond a little confused here...
@Batori - It might help if you posted your ACTUAL code... not bits and pieces... but the actual code.... all of the BGW events, plus how you're starting it, how you're opening the form, and your module code that is doing the work.
-tg
-
Mar 21st, 2014, 01:39 PM
#20
Re: background worker progress changed
Ok... I see the problem... in the sub, you need a reference to the BGW in order to call it's report process method... simple... pass it in...
Code:
Public Function CALCULATE_NUMBERS(num as integer, someBGW as BackGroundWorker)
' Do some work
someBGW.ReportProgress(10, "string")
End Fucntion
Code:
Private Sub WorkerL_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles WorkerL.DoWork
CALCULATE_NUMBERS(selected_numbers, WorkerL)
End Sub
-tg
-
Mar 21st, 2014, 02:20 PM
#21
Re: background worker progress changed
Why is the backgroundworker not located on the form any way.
-
Mar 21st, 2014, 03:22 PM
#22
Member
Re: background worker progress changed
Don't forget, if you are accessing a control outside of the thread it was created on you SHOULD be using a delegate to update that control!
First things first make sure the backgroundworker is correctly configured to report progress.
In the dowork event reference the background worker and call the reportprogress function.
(Percentages are calculated easily via NumberProcessed / NumberTotal * 100)
In the backgroundworker reportprogress event reference a delegate IF accessing a "foreign" control:
Example
Code:
Public Sub MyUpdateControlInvoke(Byval MyProperty As SomePropertyType)
If MyControl.InvokeRequired Then
MyControl.invoke(New MyUpdateControlDelegate(AddressOf MyUpdateControlInvoke), MyProperty)
Else
MyControl.Value = MyProperty
End If
End Sub
Public Delegate Sub MyUpdateControlDelegate(Byval MyProperty As SomePropertyType)
You can then use this delegate to access a control no matter what thread you start by using:
Code:
MyUpdateControlInvoke(MyProperty)
Hope this makes sense
-
Mar 21st, 2014, 04:25 PM
#23
Thread Starter
Hyperactive Member
Re: background worker progress changed
@ident ... it is.
@tg...i will try what you suggest!
EDIT:
@tg...i tried your last solution but still it did not work ... i still get the string in a msgbox but not in a textbox, label, ecc...
the first soulition has been tried to but with my function, and in that case it does the calculating but it does not give the reporting back.
@duckles ... tried your solution, adding it to module but still, did not work ...
I noticed that even if i put the following code in ProgressChanged ... it won't display it .. but again, a message box will!
I even tried to display progress percentage...still, no luck ... only a msgbox will display the progress percentage.
Code:
txtDebug.Text = "test"
Last edited by batori; Mar 22nd, 2014 at 02:58 AM.
Thanks for helping me out.
-
Mar 22nd, 2014, 02:10 PM
#24
Re: background worker progress changed
Read Johns articles on managing data with multiple forms and default form instances
http://jmcilhinney.blogspot.co.uk/
We are trying to help you yet you avoid answers. Why does it need to be in a module.
-
Mar 22nd, 2014, 02:16 PM
#25
Re: background worker progress changed
Originally Posted by duckles
Don't forget, if you are accessing a control outside of the thread it was created on you SHOULD be using a delegate to update that control!
First things first make sure the backgroundworker is correctly configured to report progress.
In the dowork event reference the background worker and call the reportprogress function.
(Percentages are calculated easily via NumberProcessed / NumberTotal * 100)
In the backgroundworker reportprogress event reference a delegate IF accessing a "foreign" control:
Example
Code:
Public Sub MyUpdateControlInvoke(Byval MyProperty As SomePropertyType)
If MyControl.InvokeRequired Then
MyControl.invoke(New MyUpdateControlDelegate(AddressOf MyUpdateControlInvoke), MyProperty)
Else
MyControl.Value = MyProperty
End If
End Sub
Public Delegate Sub MyUpdateControlDelegate(Byval MyProperty As SomePropertyType)
You can then use this delegate to access a control no matter what thread you start by using:
Code:
MyUpdateControlInvoke(MyProperty)
Hope this makes sense
No. The whole point of the BGW is to make threading and delegation easy. Also there is no need to create your own custom delegate. You would never invoke like this using the BGW.
Side point since no custom delegate is needed.
vb Code:
Public Class Form1 Public Sub MyUpdateControlInvoke(ByVal MyProperty As SomePropertyType) Me.Invoke(Sub() MyControl.Value = MyProperty) End Sub End Class
or if you wanted to check invoke needed.
vb Code:
Public Class Form1 Public Class SomePropertyType End Class Public Sub MyUpdateControlInvoke(ByVal MyProperty As SomePropertyType) If Me.InvokeRequired Then Me.Invoke(New Action(Of SomePropertyType)(AddressOf MyUpdateControlInvoke)) Else ' what ever End If End Sub End Class
-
Mar 22nd, 2014, 02:43 PM
#26
Member
Re: background worker progress changed
Batori: Please clarify.
Are you trying to get a progress percentage OR are you trying to display a progress "step"?
Also:
Code:
Private Sub why()
BackgroundWorker1.ReportProgress(10, DateTime.Now.ToString("HH:mm:ss.f"))
End Sub
Should be inside of the DoWork event.
Please clarify further.
-
Mar 22nd, 2014, 07:43 PM
#27
Thread Starter
Hyperactive Member
Re: background worker progress changed
Well, i'm having that function in a module because im used to have functions there and not "mess up" with my form code.
@duckles , withing your code i'm trying to get
Code:
DateTime.Now.ToString("HH:mm:ss.f")
which equals
and i really do get that part of a code but only in a msgbox in frmMain.
..out of curiosity i tried to get in a textbox something like
Code:
txtDebug.text = e.ProgressPercentage
and that too did not show in the textbox but only in a msgbox
i could put the function in the main form code and then it would work, as tested ... but i'm not sure why it does work "partially" when the function is stored in a module, does give results in a msgbox and not in any other txtbox, label, ecc....
@ident: how should i report then progress from a public function stored in a module when taht function is called from a background worker stored in the main form?
edit:
not even this works when put in progresschanged:
Code:
txtDebug.Text = txtDebug.Text & "test"
Last edited by batori; Mar 22nd, 2014 at 08:23 PM.
Thanks for helping me out.
-
Mar 22nd, 2014, 11:13 PM
#28
Re: background worker progress changed
WHY IS IT IN A module. I guess you not bothered to read that last link i provided. Its so frustrating when im helping you and your sticking your fingers in your ears and saying "im not listening".
-
Mar 23rd, 2014, 03:35 AM
#29
Thread Starter
Hyperactive Member
Re: background worker progress changed
It doesn't really matter what the function is...
Code:
Public Function CALCULATE_NUMBERS() As String
Dim a As Integer = 0
a = a + 1
frmMain.WorkerL.ReportProgress(10, a)
End Function
Still the same thing when the function is stored in a module. Msgbox from frmMain WorkerL progresschanged gives the result back, txtbox and others...don't.
I do get the result in a txtbox on RunWorkerCompleted but never on progresschanged.
Thanks for helping me out.
-
Mar 23rd, 2014, 08:34 AM
#30
Re: background worker progress changed
Originally Posted by batori
Well, i'm having that function in a module because im used to have functions there and not "mess up" with my form code.
1) put the function back in the form - there is zero reason for it to be in a module.
2) there's no reason for it to be a function, it should be a sub - it doesn't even return anything.
3) Just use bgw.ReportProgress... no need to specify the form or anything.
4) You ReportProgress event should then work.
-tg
-
Mar 23rd, 2014, 08:47 AM
#31
Thread Starter
Hyperactive Member
Re: background worker progress changed
By having the function/sub on the main form, everything works fine ....
But , still....im interested why do i get the report in a msgbox and not in any other textbox , label, ecc ... i'm frustrated
Thanks for helping me out.
-
Mar 23rd, 2014, 11:17 AM
#32
Re: background worker progress changed
That last sentence touches on an important point and a source of confusion for some. There’s no point updating the default instance of a form class if you haven’t actually displayed the default instance of that class in the first place. This is one of the reasons I say that, if you’re going to use default instances at all, you should use them all the time. For instance, if you create a form and display it like this:
Dim myForm As New SomeForm
myForm.Show()
then you can’t then refer to the default instance of the SomeForm class and expect the form on your screen to be affected. You’ve created one instance and displayed that, then the system creates the default instance. They are two different objects so making changes to one will not affect the other. If you’re going to make changes by referring to the default instance then you need to have displayed the default instance in the first place:
-
Mar 23rd, 2014, 01:02 PM
#33
Thread Starter
Hyperactive Member
Re: background worker progress changed
ident, i really don't understand what are you saying...
Regardless, i found a solution....still, having the function stored in a module!
Here it is :
Start with a button to trigger the worker...all in a form
Code:
Private Sub cmdSTART_Click(sender As Object, e As EventArgs) Handles cmdSTART.Click
WorkerL.ReportProgress(10, CALCULATE_NUMBERS)
End Sub
Private Sub WorkerL_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles WorkerL.DoWork
CALCULATE_NUMBERS()
End Sub
Private Sub WorkerL_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles WorkerL.ProgressChanged
txtDebug.Text = e.UserState
End Sub
' function stored in module
Code:
Public Function CALCULATE_NUMBERS() As String
Dim a As Integer = 0
a = a + 1
CALCULATE_NUMBERS = a
End Function
Thanks for helping me out.
-
Mar 23rd, 2014, 03:54 PM
#34
Re: background worker progress changed
do you have any interest in learning?
-
Mar 23rd, 2014, 04:41 PM
#35
Re: background worker progress changed
I don't see how that could possibly work...
Wait.... oh my...
Nope.... no... not gonna do it... I'm jsut going to bite my tongue... Not going to say anything further.
-tg
-
Mar 23rd, 2014, 04:50 PM
#36
Re: background worker progress changed
does the op even look at any replys?
2) there's no reason for it to be a function, it should be a sub - it doesn't even return anything.
-
Mar 23rd, 2014, 05:21 PM
#37
Re: background worker progress changed
Don't know. No longer care. I'm unsubscribing from this thread. It's gone way too long for what should have been a simple issue with a simple fix.
-tg
-
Mar 23rd, 2014, 05:23 PM
#38
Re: background worker progress changed
100% AGREE , un scribing also
-
Feb 23rd, 2018, 04:48 PM
#39
New Member
Re: background worker progress changed
@dbasnett - Thank you for the simple and effective approach to this problem. Old threads can indeed be useful.
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
|