更新批量数据会冻结屏幕

时间:2016-02-15 03:07:55

标签: c# async-await

我正在尝试更新大量数据......当我点击更新按钮时,屏幕会冻结......我敢打赌等待这个过程完成..

没有错误:我想要的是jquery.promise(),我可以按顺序执行它们。但我仍然不知道C#中是否有类似的代码......我也曾尝试task.factory,但仍然无法掌握这个想法......提前谢谢

//first execution
gvPriceListDetails.Visible = false;
lblLoading.BringToFront();
lblLoading.Visible = true;

//wait for the updateprocess to complete
if(_svc.UpdatePriceList(_model))
{
    UiHelpers.ShowSuccessForm("Price Lists Successfully Updated!");
    EditingMode(false);
    ShowDetails(_model.PricelistID);  
}


//execute when update process is complete
lblLoading.Visible = false;
gvPriceListDetails.Visible = true;

我正在使用C#,这是一个Windows窗体应用程序。

3 个答案:

答案 0 :(得分:3)

试试这个:

var result = await Task.Run(() => UpdatePriceList(_model));

答案 1 :(得分:2)

您可以使用BackgroundWorker。 (*您需要先申报并初始化地面工作人员 关注link

就像

//Upload process calling method
private void Upload(_model)
{
   //calling BG worker
   UploadBGWorker.RunWorkerAsync(new Arguments(_model))
}

UploadBGWorker_DoWorker(Arguments e)
{
   Model _model = e.model;//first execution
   gvPriceListDetails.Visible = false;
   lblLoading.BringToFront();
   lblLoading.Visible = true;

//wait for the updateprocess to complete
   if(_svc.UpdatePriceList(_model))
   {
      UiHelpers.ShowSuccessForm("Price Lists Successfully Updated!");
      e.Result = true;
   }
}

UploadBGWorker_RunWorkerCompleted(Arguments e)
{
   if(e.Result != null && e.Result == true)
   {
      //execute on successful upload
      EditingMode(false);
      ShowDetails(_model.PricelistID);  
   }
   //execute when update process is complete
   lblLoading.Visible = false;
   gvPriceListDetails.Visible = true;
}

答案 2 :(得分:1)

你看过以下Xelom发布的链接对我有意义吗? 我希望这可以帮助你。谢谢,

Save Records in Database asynchronously Or Parallely in .net c#

   public async List<SearchedItems> SearchItems(string ItemToSearch, string AuthenticationToken)
{
}

然后你可以等待saveToDb任务:

 var result = await Task.Factory.StartNew(()=> saveToDb(_list));