我必须调用一个如下工作的Web API:
我遇到了问题。 3,我尝试了Thread.Sleep
,但冻结了用户界面。
如何在不冻结UI的情况下等待任务?
public override async Task Execute(Progress<double> progress, string id)
{
FileResponse upload = await Queries.FileUpload(id, FileName, progress);
upload.ThrowIfUnsucessful();
FileResponse analyze = await Queries.AnalyzeTempo(id, upload);
analyze.ThrowIfUnsucessful();
FileResponse status;
do
{
status = await Queries.FileStatus(id, analyze);
status.ThrowIfUnsucessful();
Thread.Sleep(TimeSpan.FromSeconds(10));
} while (status.File.Status != "ready");
AnalyzeTempoResponse response = await Queries.FileDownload<AnalyzeTempoResponse>(id, status);
response.ThrowIfUnsucessful();
Action(response);
}
编辑:这就是我调用任务的方式
async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var fileName = @"d:\DJ SRS-Say You Never Let Me Go.mp3";
TempoAnalyzeTask task = new TempoAnalyzeTask(fileName, target);
await task.Execute(new Progress<double>(ProgressHandler), Id);
}
private AnalyzeTempoResponse _response;
private void target(AnalyzeTempoResponse obj)
{
_response = obj;
}
答案 0 :(得分:6)
对于最小的更改,只需切换到Task.Delay
和await
结果即可。与其他三个await
FileResponse status;
do
{
status = await Queries.FileStatus(id, analyze);
status.ThrowIfUnsucessful();
await Task.Delay(TimeSpan.FromSeconds(10));
} while (status.File.Status != "ready");