[英]Unresponsive UI while using Async & Await
I am trying to use async
and await
in my coding while I transfer big amounts of data from my WCF service to my WPF client application. 当我将大量数据从WCF服务传输到WPF客户端应用程序时,我尝试在编码中使用async
并await
。 Now it does take about 2-3 minutes to load the data, but now I struggle with my UI that's unresponsive for that total time. 现在确实需要大约2-3分钟的时间来加载数据,但是现在我的UI在总时间内没有响应 ,这让我感到很困惑 。 It's not very user-friendly. 它不是非常用户友好。 Is there a way to get my my UI responsive while the data loads with the coding that I currently have? 有什么方法可以让我的UI响应,同时数据以当前的编码加载?
public pgSysproStock()
{
InitializeComponent();
SysproStock.WindowState = WindowState.Normal;
this.UpdateStockAsync();
}
private async void UpdateStockAsync()
{
dgSysproStock.IsEnabled = false;
using (TruckServiceClient TSC = new TruckServiceClient())
{
var allStock = await TSC.GetSysproStockAsync();
dgSysproStock.ItemsSource = allStock.Select(item =>
new AllStock
{
Id = item.Id,
StockCode = item.StockCode,
Description = item.Description,
ConvFactAltUom = item.ConvFactAltUom,
ConvMulDiv = item.ConvMulDiv,
ConvFactOthUom = item.ConvFactOthUom,
MulDiv = item.MulDiv,
Mass = item.Mass,
Updated_Supplier = item.Updated_Supplier,
CycleCount = item.CycleCount,
ProductClass = item.ProductClass.ToString(),
UnitCost = item.UnitCost,
Discount = item.Discount,
Warehouse = item.Warehouse,
MinimumStock = item.MinimumStock,
MaximumStock = item.MaximumStock,
StockForNow = item.StockForNow,
CoilWidth = item.CoilWidth,
SheetCoilLength = item.SheetCoilLength,
MaterialThickness = item.MaterialThickness
}).ToArray();
dgSysproStock.IsEnabled = true;
}
}
Thank you for any advice! 感谢您的任何建议! :D :D
It seems that your operation is initiating operation incorrectly. 看来您的操作错误地启动了操作。 I suggest you create handler On Load or some other event for your window, mark it async and do the call from there. 我建议您为窗口创建“加载时”处理程序或其他事件,将其标记为异步并从那里进行调用。 Should be simple as this: 应该这样简单:
private async void Form1_Load(object sender, EventArgs e)
{
dgSysproStock.IsEnabled = false;
using (TruckServiceClient TSC = new TruckServiceClient())
{
var allStock = await TSC.GetSysproStockAsync();
dgSysproStock.ItemsSource = allStock.Select(item =>
new AllStock
{
...
}).ToArray();
dgSysproStock.IsEnabled = true;
}
}
Note that I'm using Form.Load event but you may use different. 请注意,我正在使用Form.Load事件,但您可以使用其他事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.