user2386636
user2386636

Reputation:

Background Worker ReportProgress method

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.

Upvotes: 6

Views: 1137

Answers (3)

Matthew Watson
Matthew Watson

Reputation: 109567

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");

Upvotes: 4

John Willemse
John Willemse

Reputation: 6698

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.

Upvotes: 2

RobSiklos
RobSiklos

Reputation: 8849

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

Upvotes: 1

Related Questions