6
votes

The ReportProgress method takes in 2 parameters. One's an int and one's a user state. I am passing some string parameters into the method for some processsing purposes and have no need for the int.

Is there a way to omit passing the first int without having the redundancy to call the report progress method with a ReportProgress([randomInt], "MyString")? Just for code cleaning purposes.

3
Nope, just pass 0. Or a const called dummyProgress if you prefer. :) - Matthew Watson
Is there a way to maybe create like an override method? This "useless parameter" thing is bugging me a little =) - user2386636
Haha ok give me a sec. :) - Matthew Watson
Ok I wrote an override method for you to use as an extension method (I guess that's what you meant by an override method). - Matthew Watson

3 Answers

4
votes

You can create an extension method for BackgroundWorker like so:

public static class BackgroundWorkerExt
{
    public static void ReportProgress(this BackgroundWorker self, object state)
    {
        const int DUMMY_PROGRESS = 0;
        self.ReportProgress(DUMMY_PROGRESS, state);
    }
}

Then you will be able do do the following (as long as the parameter is NOT an int, otherwise the normal ReportProgress(int) will be called):

_backgroundWorker.ReportProgress("Test");
2
votes

The ReportProgress method needs to take an int, since it raises the ProgressChanged event, which has a parameter ProgressChangedEventArgs, which in turn has a property ProgressPercentage.

Simply pass 0 to the method or, as RobSiklos suggests, write an extension method that will call ReportProgress with a 0.

1
votes

You could create an extension method which hides the first parameter (just calls the real method with 0)