locked
foreach statement and webservice in async RRS feed

  • Question

  • I am not sure how to handle foreach statement with web service in async as below:

    My questions will be:

    1. Will the foreach statement wait for the UpdateTransaction finishes before looping the next item?

    Or How do I ensure foreach will execute one at a time as web service may take longer time.

      foreach( var _line in Orders)

     {

        //-- pass to data webservice

         UpdateTransaction( _line.Customer, _line.Qty)

      }

     

    private async Task<bool> UpdateTransaction(customer, qty)
    {

         // call webservice here

    }

    Thanks

    Tuesday, December 31, 2013 6:16 AM

Answers

  • 1. yes with the await in front the UpdateTransaction the foreach will wait till updatetransaction is completed (if proper implemented ofcourse).

    2. it will throw an exception and break the foreach loop. you can prevent this by adding try catch in the loop. same as with normal synchronize code.


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Anne Jing Tuesday, January 7, 2014 7:15 AM
    Thursday, January 2, 2014 2:24 PM

All replies

  • no this way it wont wait; rewrite the foreach as:

    foreach( var _line in Orders)
    
     {
    
        //-- pass to data webservice
    
        await UpdateTransaction( _line.Customer, _line.Qty)
    
      }
    


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, December 31, 2013 12:30 PM
  • Can you explain exactly what you are trying to achieve?

    If you want to wait for each UpdateTransaction to complete you can make your method async and put Await before the UpdateTransaction call.

    Is there a reason why you don't want to download everything simultaneously for efficiency?


    I'm a self-taught noob amateur. Please take this into account when responding to my posts or when taking advice from me.

    Wednesday, January 1, 2014 4:32 AM
  • Actually I want to send data to the Backend Server via Webservice.

    I get data from local SQLite Database and use foreach statement to loop all the return result back to Server.

    foreach( var _line in Orders)

     {

        //-- pass data to webservice

        await UpdateTransaction(_line_Id, _line.Customer, _line.Qty)

      }

     

    private async Task  UpdateTransaction(customer, qty)
    {

         // call webservice here

    }

    I want to achieve below as the web service may take some time to process over the backend side

    1. will using await in foreach ensure it will loop the next time after the call for webservice is completed.

    2. What if the webservice has error over the backend side, how to capture which line Id has not be completed and try again and continue for say 2 time.

    Thanks

    Thursday, January 2, 2014 2:10 AM
  • 1. yes with the await in front the UpdateTransaction the foreach will wait till updatetransaction is completed (if proper implemented ofcourse).

    2. it will throw an exception and break the foreach loop. you can prevent this by adding try catch in the loop. same as with normal synchronize code.


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Anne Jing Tuesday, January 7, 2014 7:15 AM
    Thursday, January 2, 2014 2:24 PM