显示进度对话框只有一个任务,在指定时间内没有完成 [英] Displaying progress dialog only if a task did not finish in specified time

查看:132
本文介绍了显示进度对话框只有一个任务,在指定时间内没有完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的服务,上传图片,我用它是这样的:

I have a little service that uploads an image, and I use it like this:

ImageInfo result = await service.UploadAsync(imagePath);

我想要做的是显示一个进度对话框,但只有当上载操作需要更多的比,也就是说,2秒。上传完成后,我想关闭进度对话框。

What I'd like to do is to display a progress dialog, but only if the upload operation takes more than, say, 2 seconds. After the upload is done, I want to close the progress dialog.

我在使用任务/ ContinueWith粗的解决方案,但我希望有更多的优雅的方式。

I made a crude solution using Task/ContinueWith, but I was hoping for a more "elegant" way.

我怎样才能做到这一点使用异步/计谋

How can I achieve this using async/await?

推荐答案

可能是这样呢?

var uploadTask = service.UploadAsync(imagePath);
var delayTask = Task.Delay(1000);//Your delay here
if (await Task.WhenAny(new[] { uploadTask, delayTask }) == delayTask)
{
    //Timed out ShowProgress
    ShowMyProgress();
    await uploadTask;//Wait until upload completes
    //Hide progress
    HideProgress();
}

这篇关于显示进度对话框只有一个任务,在指定时间内没有完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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