Help worker ReportProgress method

The ReportProgress method accepts 2 parameters. One int and one user state. I pass some string parameters to a method for some processes and do not need an int.

Is there a way to skip passing the first int without having redundancy to call the report progress method using ReportProgress([randomInt], "MyString") ? Only to clear the code.

+6
source share
3 answers

You can create an extension method for BackgroundWorker as follows:

 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 can do the following (as long as the parameter is NOT int, otherwise normal ReportProgress(int) will be called):

 _backgroundWorker.ReportProgress("Test"); 
+4
source

The ReportProgress method must accept int because it raises the ProgressChanged event, which has the ProgressChangedEventArgs parameter, which in turn has the ProgressPercentage property.

Just pass 0 to the method or, as RobSiklos suggests, write an extension method that will call ReportProgress from 0.

+2
source

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

+1
source

Source: https://habr.com/ru/post/945944/


All Articles