c# - How to use await in a parallel foreach? -
so sepnt better part of night trying figure out.
i fortunate introduced parallel.foreach yesterday , works want except 1 detail.
i have following:
parallel.foreach(data, (d) => { try { mymethod(d, measurements); } catch (exception e) { // logg } });
within method "mymethod" have alot of logic gets done , of fine make api calls fetch data , use async task able use "await" in order code wait untill specific part gets executed , move on:
private async void mymethod(pimdata pimdata, ienumerable<productmeasurements> measurements) { try { // alot of logic relevant part await task.whenall(executemeasurmentandchartlogic(pimdata.productnumber, entity)); await task.whenall(resourceimportmanager.handleentityimagefiles(pimdata.producttype + pimdata.productsize,swepimagetype.png, resourcefiletypes.threed, entity, linktypeid.productresource)); await task.whenall(resourceimportmanager.handleentityimagefiles(pimdata.productsketch, swepimagetype.png, resourcefiletypes.sketch, entity, linktypeid.productresource)); } catch (exception e) { // logg } }
problems:
1 starters loop finishes before code finished
2 second problem "task canceled" in alot of api calls
3 , third mentioned above, code not wait each method full execute.
i cant execute in executemeasurmentandchartlogic() method before moving forward next step.
this gives me following issues (more issues):
in method create item , add db, , item needs more info api call done inside of executemeasurmentandchartlogic() problem several items craeated , have wait rest of data not desire.
side-note: aware of crating item , adding db before data there not best practice integrating toward pim , process delicate
i want several threads running @ same time want fuill logic execute each item before moving on next method.
clarify:
several items running
each item handels all logic needs handel before moving on next part of code, noramly await.
in code above resourceimportmanager() method gets executed before executemeasurmentandchartlogic() finished. dont want.
instead of parallel.foreach used :
task task1 = task.factory.startnew(() => mymethod(data, measurements)); task.waitall(task1);
but wasnt of help
fairly new , havent been able understand doing wrong.
edit: updated problems this
edit: how executemeasurmentandchartlogic() looks like:
public async task executemeasurmentandchartlogic(string productnumber, entity entity) { try { grafgeneratormanager grafmanager = new grafgeneratormanager(); var graphmeasurmentlist = await measurmenthandler.getmeasurments(productnumber); if (graphmeasurmentlist.count == 0) return; var chart = await grafmanager.generatechart(500, 950, systemcolors.window, chartcolorpalette.earthtones, "legend", graphmeasurmentlist); await addchartsandaddtoxpc(chart, entity, productnumber); } catch (exception e) { console.writeline(e); } }
edit: background this: make call api alot of data. each item in data need make api call , data apply item.
after reading comments alos got me thinking in diffirent way. can perhaps loop through items , minor logic them , add url in task list , make seperate task executes 1 one.
will keep updated
don't use parralel.foreach
@ all. make method return task instead of void, collect task , wait them like:
task.waitall(data.select(d => mymethod(d, someparam)).toarray());
Comments
Post a Comment