asp.net - sync execution in async c# -


i want run multiple transactions parallel using async call in transaction want process task in sequence because sequence matters. use async call doesn't help, makes whole transaction async make conflict.

suppose want process data of multiple companies parallel, data of company in sequence, need productid of product detail of product first have products , detail of product using productid against company. cant product detail without productid.

task wrappedintask = task.run(() => companyasync(companyid)); public static async task companyasync(int companyid)     {       string[] products=getallproducts(compnyid);        for(int i=0; i<products.length>i; i++)           {            //get product detail here using products[i]           }     } 

i want execute getallproducts(compnyid) , execute loop if loop , getallproducts(compnyid) method exicute asynchronously there conflict. suggestion this

if getallproducts(compnyid) asynchronous method too, can wait finish, before executing loop:

string[] products = await getallproducts(compnyid); for(int = 0; < products.length; i++) {    // ... } 

anyway, don't know how current code sample manages compile. if getallproducts in fact async method, string[] products = getallproducts(compnyid) should throw error, because returns task<string[]> , not string[].

edit

you should not confuse parallelism asynchronism.

it seems want paralelly iterate on list.

async methods used if going execute inherently asynchronous operation (such i/o), otherwise not needed.

you may use system.threading.tasks.parallel class task.

//this create new threads (or not) , iterate list concurrently parallel.foreach(dt.rows, datarow => {      int companyid = //fetch companyid      company(companyid); });  //code here execute after each element has been paralelly iterated. 

please note new companyasync method signature should changed to:

public static void company(int companyid); 

for further reference on how use approach, please read:


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -