locked
multiple parameters with backgroundworker RRS feed

  • Question

  • After searching thru BOL and various forums, I've been unable to determine how to use a backgroundworker component with a procedure that contains multiple parameters. If I understand correctly, DoWorkEventArgs is used to pass parameters. However, all the examples I have found use only one. Since DoWorkEventArgs ends in an "s", I imagine it is possible to use more than one parameter. Can anyone explain this to me or point me to a multiple parameter example?

    Friday, July 28, 2006 9:55 PM

Answers

  • You only need a single object which you pass as a parameter through the RunWorkerAsync() method of the worker object.

    That parameter can be anything (just about), since it's an object. What reason would you need for multiple parameters? (the point is moot, because that's the way it is).

    If you have multiple objects you want to pass, simply wrap them in another object (e.g.):

    Class MyThreadObject
       
    public Thing as String
       
    Public Bouncer as Integer
       
    Public LoadOMoney() as decimal
    EndClass

    Dim MyParam as new MyThreadObject
    MyWorker.RunWorkerAsync(MyParam)

     

    Friday, July 28, 2006 10:28 PM
    Moderator

All replies

  • You only need a single object which you pass as a parameter through the RunWorkerAsync() method of the worker object.

    That parameter can be anything (just about), since it's an object. What reason would you need for multiple parameters? (the point is moot, because that's the way it is).

    If you have multiple objects you want to pass, simply wrap them in another object (e.g.):

    Class MyThreadObject
       
    public Thing as String
       
    Public Bouncer as Integer
       
    Public LoadOMoney() as decimal
    EndClass

    Dim MyParam as new MyThreadObject
    MyWorker.RunWorkerAsync(MyParam)

     

    Friday, July 28, 2006 10:28 PM
    Moderator
  • Reason for needing multiple parameters: I'm calling a procedure that requires multiple parameters. However, your explanation about using a wrapper resolves the issue. Thanks so much.
    Saturday, July 29, 2006 10:15 AM
  • object[] arguments = {string, int, searchOption, DirectoryInfo, ...etc};

    backgroundWorker1.RunWorkerAsync(arguments);

     

    Tuesday, November 2, 2010 3:25 PM