我有一个Windows表单应用程序,这是我的代码:
我收到了编译错误,错误信息是“System.Progress'不包含'Report'的定义,也没有接受类型为'System.Progress'的第一个参数的扩展方法'Report'(您是否缺少使用指令或程序集引用?)”
但是,如果您查看Progress类:
:
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);
}
}
我收到了编译错误,错误信息是“System.Progress'不包含'Report'的定义,也没有接受类型为'System.Progress'的第一个参数的扩展方法'Report'(您是否缺少使用指令或程序集引用?)”
但是,如果您查看Progress类:
public class Progress<T> : IProgress<T>
接口 IProgress 具有 Report 功能:
public interface IProgress<in T>
{
// Summary:
// Reports a progress update.
//
// Parameters:
// value:
// The value of the updated progress.
void Report(T value);
}
我错过了什么?
IProgress
参数而不是Progress
(消除了显式转换的需要)。这使得调用者可以提供自己的IProgress
实现。 - Bip901