12

I understand how we can pass one variable(progresspercentage) to "progresschanged" function , like so.

backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

...

worker.ReportProgress(pc);

...

private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
   this.progressBar1.Value = e.ProgressPercentage;
}

But I want to pass more variables to this function, some thing like:

worker.ReportProgress(pc,username,score);

...

private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
   this.progressBar1.Value = e.ProgressPercentage;
   this.currentUser.Value = e.UserName;  //as string
   this.score.Value = e.UserScore;  //as int
}

sorry I'm new to c#, could someone give me an example.

3 Answers 3

22

The ReportProgress method of background worker component is overloaded to pass percentage and an object typed state value:

public void ReportProgress(int percentProgress, Object userState)

In your usage requirement you can concatenate the UserName and Score with a char separator, and so pass the multiple values inside the userState parameter; and split them inside the ProgressChanged() event when it is raised. You can also create a small property based class- fill it with values and pass using the userState object typed parameter.

For a sample example of how to use the overloaded ReportProgress method, please look at the below MSDN link:

http://msdn.microsoft.com/en-us/library/a3zbdb1t.aspx

18

In case anyone is looking for a comprehensive answer:

  1. Fast & simple approach would be object[] as in:

     worker.ReportProgress(i, new object[] { pc, username, score });
    
  2. Fast & Type-safe approach would be System.Tuple<> as in:

     worker.ReportProgress(i, new System.Tuple<object, string, float>(pc, username, score));
    
  3. A better practice would be to write your custom class (or maybe inherit from System.Tuple<>).

     public class PcUsernameScore
     {
         public object PC;
         public string UserName;
         public float Score;
         public PcUsernameScore(object pc, string username, float score)
         {
             PC = pc; Username = username; Score = score;
         }
     }
    

or

    public class PcUsernameScore : System.Tuple<object, string, float>
    {
        public PcUsernameScore(object p1, string p2, float p3) : base(p1, p2, p3) { }
    }

to have something like:

    worker.ReportProgress(i, new PcUsernameScore(pc, username, score));

C# 7.1

  1. Inferred ValueTuple feature:

    worker.ReportProgress(i, (pc: "pc", username: "me", score: 0));
    

C# 9.0

  1. Best practice: To use record:

     public record PcUsernameScore(object PC, string UserName, float Score);
    
2
  • Seems like a good idea, but for some reason I couldn't get example 3 to work (deriving a custom class from System.Tuple) using .NET Framework version 4 / MS Visual Studio 2010 Professional. The definition of PcUsernameScore class gives "error CS1729: 'System.Tuple<object,string,float>' does not contain a constructor that takes 0 arguments", and the worker ReportProgress call gives "error CS1729: 'myclass.PcUsernameScore' does not contain a constructor that takes 3 arguments". But your example 2 works OK.
    – MarkU
    Mar 23, 2015 at 23:17
  • 2
    you could also pass a custom class or struct instead of a tuple
    – Eric
    Sep 8, 2016 at 19:29
7

Create a data transfer object with properties for the items you want to pass and and then pass it in as the user state. In the OO world, the answer is almost always to create another object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.