|
-
Feb 19th, 2019, 02:14 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Passing multiple arguments to BackgroundWorker.ProgressChanged
I recently learned How to pass multiple parameters to a BackgroundWorker.DoWork method. I used an object created from a class I made to hold the parameters and passed it in the DoWorkEventArgs. Today I was attempting to do the same with the ProgressChanged. I've seen some examples online but it's not working for me. Is this a normal thing to do? I dont' want to jump though hoops to "make it work". If there is a normal and accepted way to do this, I'd like to know how. If MS never intended this to do more than passing text, then I'd rather keep doing it as I am.
As it is now I create a delimited string and split it using a method in a generic text class method of my design. It's just for simple things like whether the message overwrites the last line of a multi-line TextBox or not.
-
Feb 19th, 2019, 02:33 PM
#2
Thread Starter
Frenzied Member
Re: Passing multiple arguments to BackgroundWorker.ProgressChanged
Update: MSDN said that it would also accept a Tuple. I had never heard of a Tuple before, very interesting, but I tried it and the VS complains that "Value of type 'Tuple(Of Integer, String, Boolean)' cannot be converted to 'ProgressChangedEventArgs'." It seems like no other object type can be passed to ProgressChanged.
-
Feb 19th, 2019, 02:40 PM
#3
Re: Passing multiple arguments to BackgroundWorker.ProgressChanged
Something like
Code:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
BackgroundWorker1.ReportProgress(100, New Tuple(Of Integer, String, Boolean)(100, "test", False))
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim x = DirectCast(e.UserState, Tuple(Of Integer, String, Boolean))
End Sub
should do the trick.
-
Feb 19th, 2019, 02:41 PM
#4
Re: Passing multiple arguments to BackgroundWorker.ProgressChanged
ProgressChangedEventArgs is class with two properties, you need to assign the value to (and read it from) the UserState property.
The data type is Object, so you can put anything you want in there, including a Tuple() or your own class.
-
Feb 19th, 2019, 02:42 PM
#5
Thread Starter
Frenzied Member
Re: Passing multiple arguments to BackgroundWorker.ProgressChanged
This was what I was trying, but it doesn't work. "Value of type 'Tuple(Of Integer, String, Boolean)' cannot be converted to 'ProgressChangedEventArgs'." How can it be that it works for you but not me?
-
Feb 19th, 2019, 02:43 PM
#6
Thread Starter
Frenzied Member
Re: Passing multiple arguments to BackgroundWorker.ProgressChanged
si_the_geek: I have tried my own classes and tuples. All the same. Can't convert.
-
Feb 19th, 2019, 02:47 PM
#7
Thread Starter
Frenzied Member
Re: Passing multiple arguments to BackgroundWorker.ProgressChanged
I see many examples like these online. But they don't work for me. Maybe it would help if you saw what I have. I get the same problem with my own class.
-
Feb 19th, 2019, 02:50 PM
#8
Re: Passing multiple arguments to BackgroundWorker.ProgressChanged
Did you use exactly what PlausiblyDamp showed?
The two signatures for ReportProgress are like this:
Code:
Public Sub ReportProgress (percentProgress As Integer)
Public Sub ReportProgress (percentProgress As Integer, userState As Object)
...so with his code, the error message would be odd.
edit: I see you've posted a screenshot, which has left me confused as to what could be happening.
-
Feb 19th, 2019, 02:51 PM
#9
Thread Starter
Frenzied Member
Re: Passing multiple arguments to BackgroundWorker.ProgressChanged
The screenshot is much larger than that. Why does it look so small? Try this link instead.
-
Feb 19th, 2019, 02:51 PM
#10
Re: Passing multiple arguments to BackgroundWorker.ProgressChanged
To raise the progress changed event you need to call the BackgroundWorker's ReportProgress method, the example I posted above shows an example.
In future it is better to post code here rather than screenshots as the forum tends to mangle them and make things hard to read.
-
Feb 19th, 2019, 02:52 PM
#11
Re: Passing multiple arguments to BackgroundWorker.ProgressChanged
Ah, I've got it... you changed BackgroundWorker1.ReportProgress to BackgroundWorker1_ReportProgress
Changing it back should fix it.
-
Feb 19th, 2019, 02:53 PM
#12
Thread Starter
Frenzied Member
Re: Passing multiple arguments to BackgroundWorker.ProgressChanged
I knew you might think I was a crazy newb so I had to prove it with a screenshot! :-)
Something different in 4.5.1 or VS2017 maybe?
-
Feb 19th, 2019, 02:55 PM
#13
Thread Starter
Frenzied Member
Re: Passing multiple arguments to BackgroundWorker.ProgressChanged
Sorry. I meant 4.6.1. I tried turning Option Strict off but that didn't make any difference either.
-
Feb 19th, 2019, 03:02 PM
#14
Re: Passing multiple arguments to BackgroundWorker.ProgressChanged
Like this...
Code:
Public Class BGWSample
Private WithEvents Bgw As New ComponentModel.BackgroundWorker With {.WorkerReportsProgress = True}
Public SomeObject As Object = Nothing
Dim SomeString As String = "It Worked"
Public Class ProgChangedArgs
Public _SomeObject As Object
Public _SomeString As String
'make as many 'args' as you want
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Bgw.RunWorkerAsync()
End Sub
Private Sub Bgw_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles Bgw.DoWork
Dim ProgChangedArgs As New BGWSample.ProgChangedArgs With {
._SomeObject = SomeObject,
._SomeString = SomeString}
Bgw.ReportProgress(100, ProgChangedArgs)
End Sub
Private Sub Bgw_ProgressChanged(sender As Object, e As ComponentModel.ProgressChangedEventArgs) Handles Bgw.ProgressChanged
Dim ProgChangedArgs As ProgChangedArgs = DirectCast(e.UserState, ProgChangedArgs)
MsgBox(ProgChangedArgs._SomeString)
End Sub
End Class
-
Feb 19th, 2019, 03:03 PM
#15
Thread Starter
Frenzied Member
Re: Passing multiple arguments to BackgroundWorker.ProgressChanged
Oh my. Well spotted! I can't believe I typed that in. Ug. What was I thinking. Thank you and your keep eyes very much.
-
Feb 19th, 2019, 06:20 PM
#16
Thread Starter
Frenzied Member
Re: [RESOLVED] Passing multiple arguments to BackgroundWorker.ProgressChanged
In PlausiblyDamp's wonderful example, it there a way to make the Tuples named? The best I cold come up with was to make a new Tuple object. Is there a way to name the tuples on instantiation? Does anyone know how to do that?
vb.net Code:
Dim objArgs As Tuple(Of Integer, String, Boolean) = DirectCast(e.UserState, Tuple(Of Integer, String, Boolean))
Dim objTest = (intTest:=objArgs.Item1, strTest:=objArgs.Item2, blnTest:=objArgs.Item3)
TextBox1.Text = objTest.strTest
-
Feb 19th, 2019, 06:31 PM
#17
Re: [RESOLVED] Passing multiple arguments to BackgroundWorker.ProgressChanged
Tuples are just simple classes, the easiest way is to make your own classes of the same sort, eg:
Code:
Class MyTuple
Public intTest as Integer
Public strTest as String
Public blnTest as Boolean
Public Overrides Sub New()
End Sub
Public Overrides Sub New(intTest as Integer, strTest as String, blnTest as Boolean)
Me.intTest = intTest
Me.strTest = strTest
Me.blnTest = blnTest
End Sub
End Class
(I'm tired, and think I've got syntax a little off, but it's reasonably close!)
-
Feb 19th, 2019, 06:36 PM
#18
Thread Starter
Frenzied Member
Re: [RESOLVED] Passing multiple arguments to BackgroundWorker.ProgressChanged
I get your drift. Then I might as well use that my own class on the on the other side and abandon the tuples. And with a constructor, I can do it in the argument.
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
|