1

I'm trying to update alot of data... and when I click the update button the screen freezes... I bet it waits for the process to complete..

no errors: what I want is something like jquery.promise() where I can execute them on a sequencial manner..but I still don't know if there's a similar code in C#... I also have tried the task.factory but can't still grasp the idea... thanks in advance

//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;

I am using C# and this is a windows form application.

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
Sam Teng Wong
  • 2,379
  • 5
  • 34
  • 56

3 Answers3

3

Try this:

var result = await Task.Run(() => UpdatePriceList(_model));
chinlean
  • 25
  • 3
2

you can use BackgroundWorker instead. (*you need to declare and initialize back ground worker first follow link

it would be like

//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;
}
Amit
  • 1,821
  • 1
  • 17
  • 30
1

Did you look at the link below posted by Xelom It make sense to me? I hope this helps you. Thank You,

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

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

Then you can await for saveToDb task:

 var result = await Task.Factory.StartNew(()=> saveToDb(_list));
Community
  • 1
  • 1
Nour Lababidi
  • 414
  • 4
  • 7
  • [Don't use StartNew with async/await](http://blog.stephencleary.com/2013/08/startnew-is-dangerous.html), use `Task.Run(` instead. If you use StartNew and don't pass in a `TaskScheduler` you could end up with code on the UI thread that you expect to be on a background thread. – Scott Chamberlain Feb 15 '16 at 04:32
  • @ScottChamberlain is it possible to cancel the `update` if the user tried to close the form? – Sam Teng Wong Feb 15 '16 at 05:12
  • Possibly, you need to write code that supports it via a `CancellationToken`. Go [learn about the subject](https://msdn.microsoft.com/en-us/library/dd997364(v=vs.110).aspx), if you still do not understand how to do it after reading through that article then go ask a new question. – Scott Chamberlain Feb 15 '16 at 05:19