使用异步/等待时,WPF进度用法 [英] WPF ProgressBar usage when using Async/Await

查看:1125
本文介绍了使用异步/等待时,WPF进度用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加载像这样一些数据的非常简单的任务:

 异步任务<名单<发票>> GetInvoices()
{
VAR发票=等待Task.Run(()=> db.Invoices.AsNoTracking()了ToList());
返回发票;
}



什么是与一个进度使用的最佳方法?我不需要得到的百分比加载(尽管这将是有益的),我只需要显示/隐藏在启动和加载数据的完成进度条。有没有一种方法或许创建完成或结束的方法?



我很新的异步/等待这么心慈手软,请!


< DIV CLASS =h2_lin>解决方案

假设 GetInvoices 被调用一些UI事件处理程序,并有一些所谓的进度条控件进度,这是很简单的:

 专用异步无效OnButton1Click(..)
{
ProgressBar.IsIndeterminate = TRUE;
VAR的结果=等待GetInvoices();
ProgressBar.IsIndeterminate = FALSE; //也许隐藏它,太
// TODO:做的东西有结果
}

如果你想显示实际进展情况,看一看的 进度< T> 和的 IProgress< T>


I have a very simple task that loads some data like so:

async Task<List<Invoice>> GetInvoices()
{
    var invoices = await Task.Run(() => db.Invoices.AsNoTracking().ToList());
    return invoices;
}

What would be the best way to use this with a progressbar? I don't need to get the percentage loaded (although that would be useful), I simply need to show/hide the progressbar at start and finish of loading data. Is there a way to create a completed or finished method perhaps?

I am very new to async/await so kid gloves please!

解决方案

Assuming GetInvoices gets called in some UI event handler and there is some progress bar control called ProgressBar, it's really simple:

private async void OnButton1Click(..)
{
    ProgressBar.IsIndeterminate = true;
    var result = await GetInvoices();
    ProgressBar.IsIndeterminate = false; // Maybe hide it, too
    // TODO: Do stuff with result
}

If you want to show actual progress, take a look at Progress<T> and IProgress<T>.

这篇关于使用异步/等待时,WPF进度用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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