Results 1 to 18 of 18

Thread: [RESOLVED] Passing multiple arguments to BackgroundWorker.ProgressChanged

  1. #1

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Resolved [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.

  2. #2

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    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.

  3. #3
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    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.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  5. #5

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    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?

  6. #6

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: Passing multiple arguments to BackgroundWorker.ProgressChanged

    si_the_geek: I have tried my own classes and tuples. All the same. Can't convert.

  7. #7

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    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.
    Name:  2019-02-19_11-45-22.jpg
Views: 2177
Size:  20.2 KB

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  9. #9

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: Passing multiple arguments to BackgroundWorker.ProgressChanged

    The screenshot is much larger than that. Why does it look so small? Try this link instead.

  10. #10
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    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.

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  12. #12

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    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?

  13. #13

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    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.

  14. #14
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    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

  15. #15

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    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.

  16. #16

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    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:
    1. Dim objArgs As Tuple(Of Integer, String, Boolean) = DirectCast(e.UserState, Tuple(Of Integer, String, Boolean))
    2. Dim objTest = (intTest:=objArgs.Item1, strTest:=objArgs.Item2, blnTest:=objArgs.Item3)
    3. TextBox1.Text = objTest.strTest

  17. #17
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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!)

  18. #18

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    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
  •  



Click Here to Expand Forum to Full Width