Progress <T> has no report function
I have a windows application, this is my code:
private async void btnGo_Click(object sender, EventArgs e) { Progress<string> labelVal = new Progress<string>(a => labelValue.Text = a); Progress<int> progressPercentage = new Progress<int>(b => progressBar1.Value = b); // MakeActionAsync(labelVal, progressPercentage); await Task.Factory.StartNew(()=>MakeActionAsync(labelVal,progressPercentage)); MessageBox.Show("Action completed"); } private void MakeActionAsync(Progress<string> labelVal, Progress<int> progressPercentage) { int numberOfIterations=1000; for(int i=0;i<numberOfIterations;i++) { Thread.Sleep(10); labelVal.Report(i.ToString()); progressPercentage.Report(i*100/numberOfIterations+1); } } I get a compilation error: "System.Progress" does not contain a definition for "Report", and the "Extension" extension method cannot be found that takes the first argument of the type "System.Progress" (do you miss the using directive or the assembly reference?) "
but if you look at the Progress class:
public class Progress<T> : IProgress<T> and the IProgress interface has a Report function:
public interface IProgress<in T> { // Summary: // Reports a progress update. // // Parameters: // value: // The value of the updated progress. void Report(T value); } What am I missing?
Progress<T> implemented a method with an explicit implementation of the interface. Therefore, you cannot access the Report method with an instance of type Progress<T> . You need to give it IProgress<T> to use Report .
Just change the announcement to IProgress<T>
IProgress<int> progressPercentage = new Progress<int>(b => progressBar1.Value = b); Or use a throw
((IProgress<int>)progressPercentage).Report(i*100/numberOfIterations+1); I prefer the previous version, the latter is inconvenient.
As shown in the documentation , this method is implemented using an explicit interface implementation. This means that it is hidden if you are not using the interface to access the method.
Explicit interface implementations are used to make some properties and methods visible when accessing the interface, but not in any derived class. Thus, you only “see” them when using IProgress<T> as your variable type, but not when using Progress<T> .
Try the following:
((IProgress<string>)progressPercentage).Report(i*100/numberOfIterations+1); Or when you only need to reference the properties and methods available in the interface declaration:
IProgress<string> progressPercentage = ...; progressPercentage.Report(i*100/numberOfIterations+1);