实现IProgress<in T>接口的Progress<int T>类,接收一个Action<T>,通过这个委托显示进度。
服务器端:
public async Task<string> ALongTimeTask()
{
//方法1
//var p = new Progress<int>();
//p.ProgressChanged += (a,count) =>
//{
// Clients.Caller.progressChanged(count);
//};
// var ret = await DoLongRunningThing(p);
//方法2
var progress = new Progress<int>(percent => Clients.Caller.progressChanged(percent));
var ret = await DoLongRunningThing(progress);
return ret;
}
public async Task<string> DoLongRunningThing(IProgress<int> progress)
{
for (int i = 0; i <= 100; i += 5)
{
await Task.Delay(200);
progress.Report(i);
}
return "Job complete!";
}
客户端:
ChatHubProxy.On("progressChanged", (count) =>
{
//display current count;
WriteInfo("Percent: " + count);
});
ChatHubProxy.Invoke("ALongTimeTask");