无冻结替代的Thread.Sleep的任务在里面等着 [英] No-freezes alternative to Thread.Sleep for waiting inside a Task

查看:737
本文介绍了无冻结替代的Thread.Sleep的任务在里面等着的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要调用的Web API,它的工作原理如下:




  1. 上传一首歌

  2. 关于那首歌的请求特定分析

  3. ,该过程完成

  4. 检索并返回结果



我有没有问题。 3,我试过 Thread.sleep代码但冻结UI。



我怎样才能在等待任务不冻结UI

 公众覆盖异步任务执行(进度<双>的进步,字符串ID)?
{
FileResponse上传=等待Queries.FileUpload(ID,文件名,进度);
upload.ThrowIfUnsucessful();

FileResponse分析=等待Queries.AnalyzeTempo(ID,上传);
analyze.ThrowIfUnsucessful();

FileResponse状态;

{
状态=等待Queries.FileStatus(ID,分析);
status.ThrowIfUnsucessful();
Thread.sleep代码(TimeSpan.FromSeconds(10));
},而(status.File.Status =准备好了!);

AnalyzeTempoResponse响应=等待Queries.FileDownload< AnalyzeTempoResponse>(ID,状态);
response.ThrowIfUnsucessful();

动作(响应);
}



编辑:这就是我所说的任务

 异步无效MainWindow_Loaded(对象发件人,RoutedEventArgs E)
{
变种文件名= @D:\DJ SRS-说你从来不让我Go.mp3
TempoAnalyzeTask任务=新TempoAnalyzeTask(文件名,目标);
等待task.Execute(新进展<双>(ProgressHandler),ID);
}
私人AnalyzeTempoResponse _response;

私人无效的目标(AnalyzeTempoResponse OBJ)
{
_response = OBJ;
}


解决方案

有关最小的改动只需切换到 Task.Delay 等待的结果。这将不再阻止你的用户界面,而你等待10秒钟,就像你的其他三个的await 取值

  FileResponse状态; 

{
状态=等待Queries.FileStatus(ID,分析);
status.ThrowIfUnsucessful();
等待Task.Delay(TimeSpan.FromSeconds(10));
},而(status.File.Status =准备好了!);


I have to call a web API that works as follows :

  1. upload a song
  2. request a particular analysis on that song
  3. wait that the process is finished
  4. retrieve and return the result

I am having a problem with no. 3, I've tried Thread.Sleep but that freezes the UI.

How can I wait in a task without freezing the 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);
}

EDIT : this is how I call the task

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;
}

解决方案

For minimal changes just switch to Task.Delay and await the result. This no longer blocks your UI while you wait 10 seconds just like your other three awaits

FileResponse status;
do
{
    status = await Queries.FileStatus(id, analyze);
    status.ThrowIfUnsucessful();
    await Task.Delay(TimeSpan.FromSeconds(10));
} while (status.File.Status != "ready");

这篇关于无冻结替代的Thread.Sleep的任务在里面等着的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆