3

I'm writing a desktop client that allows the user to upload a spreadsheet with products. The spreadsheet is then parsed by the API and products are either created or updated and saved using the ContentService.

Since this process is really slow my first solution was to set the timeout of the client webrequest to 5 minutes, but i don't like that solution. Instead i want to make the creation/updates in another thread. This solution is not working since it throws an ArgumentNullException when using ContentService.Save(IList).

A snipppet of my current code:

var items = parser.Parse(); // Parse spreadsheet
new Task(() =>
{
  var products = new List<IContent>();
  for (var item in items)
  {
    if (item.IsValid())
    {
        var product = CreateOrUpdateProduct(parentId, item);
        products.Add(product);
    }
  }
  ApplicationContext.Current.Services.ContentService.Save(products);
).Start();

Is it possible to do this?

frmi
  • 508
  • 1
  • 7
  • 21
  • Well, you can use [async / await](https://msdn.microsoft.com/en-us/library/hh191443.aspx) ... It allows you to perform task on background thread while awaiting it. When task is finished the handle will return to method and execute the rest of it. – Fabjan Apr 22 '16 at 08:38
  • @fabjan, Doesn't that solution still make the client wait for the task to finish? – frmi Apr 22 '16 at 08:58
  • Not really, it is awaited by state machine on server side, but client's UI thread is released and therefore UI doesn't freeze. From client point of view he clicked some button and got some instant respond from app. – Fabjan Apr 22 '16 at 09:16

0 Answers0