I have a Hub class, which has a long running method, I need to display a progress bar while it's working.
I read this article and I think it is possible to use IProgress interface in async methods to send long running operation status.
I write a method like this:
public async Task<string> GetServerTime(IProgress<int> prog)
{
await Task.Run(() => {
for (int i = 0; i < 10; i++)
{
prog.Report(i * 10);
System.Threading.Thread.Sleep(200);
}
});
return DateTime.Now.ToString();
}
And I try to invoke the method like this:
var appHub = $.connection.appHub;
$.connection.hub.start();
appHub.server.getServerTime()
.done(function (time) {
alert(time);
});
But I don't know how can I get the progress reports.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…