在使用后台工作器时失去UI功能
本文关键字:失去 UI 功能 工作 后台 | 更新日期: 2023-09-27 18:06:38
我对使用不同的线程和使用BackgroundWorker
很陌生,所以我还没有完全理解一切。我已经张贴了所有的编码,我有从客户端程序下面。
问题
,我有是,即使我使用BackgroundWorker
,我仍然失去了所有的UI的功能,而我从我的WCF服务加载数据到我的WPF客户端应用程序。加载数据大约需要2-3分钟(大约+- 1500行数据),因此这会导致非常糟糕的用户体验。
是否有任何明显的事情,我可能会错过让UI工作,而数据加载到datagrid
?任何建议将不胜感激,谢谢!:)
更新代码:
public partial class pgSysproStock : Window
{
public pgSysproStock()
{
InitializeComponent();
SysproStock.WindowState = WindowState.Normal;
dgSysproStock.IsEnabled = false;
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.WorkerReportsProgress = true;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerAsync();
}
private readonly BackgroundWorker worker = new BackgroundWorker();
private async void worker_DoWork(object sender, DoWorkEventArgs e)
{
using (TruckServiceClient TSC = new TruckServiceClient())
{
List<AllStock> allStock = new List<AllStock>();
this.Dispatcher.Invoke((Action)(() =>
{
prgStockProgress.Visibility = Visibility.Visible;
}));
foreach (var item in await TSC.GetSysproStockAsync())
allStock.Add(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
});
e.Result = allStock.ToArray();
}
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.prgStockProgress.Value = e.ProgressPercentage;
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
dgSysproStock.ItemsSource = (AllStock[])e.Result;
dgSysproStock.IsEnabled = true;
}
}
EDIT:我还删除了async
&await
从我的编码方法,但数据似乎仍然没有通过。
我在这里添加了一个断点: foreach (var item in await TSC.GetSysproStockAsync())
和我无法通过TSC.GetSysproStock())
代码。它为什么这样做?
正确使用async/await:
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().ToArray();
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;
}
}
BackgroundWorker不应该这样使用。尝试将数据传递给RunWorkerCompleted事件处理程序,并从那里更新UI。
删除:
this.Dispatcher.Invoke((Action)(() =>
{
dgSysproStock.ItemsSource = (allStock.ToArray());
dgSysproStock.IsEnabled = true;
}));
在DoWork事件处理程序中,将Result设置为数据:
e.Result = allStock.ToArray();
然后在RunWorkerCompleted事件处理程序中:
dgSysproStock.ItemsSource = (AllSTock[])e.Result;
dgSysproStock.IsEnabled = true;