Wednesday 15 August 2012

c# - async and await while adding elements to List -



c# - async and await while adding elements to List<T> -

i wrote method, adds elements list many sources. see below:

public static async task<list<searchingitem>> getitemstoselect() { list<searchingitem> searchingitems = new list<searchingitem>(); foreach (place place in await getplaces()) { searchingitems.add(new searchingitem() { idfromrealmodel=place.id, nametodisplay=place.fullname, extrainformation=place.name, typeofsearchingitem=typeofsearchingitem.place }); } foreach (group grouping in await getgroups()) { searchingitems.add(new searchingitem() { idfromrealmodel = group.id, nametodisplay = group.name, extrainformation = group.typename, typeofsearchingitem = typeofsearchingitem.group }); } homecoming searchingitems; }

i tested method , works propertly. suppose works propertly, because getplaces method homecoming 160 elements , getgroups homecoming 3000. but, wondering if work if methods homecoming elements in same time. should lock list searchingitems ?

thank advice.

your items not run @ same time, start getplaces(), stop , wait getplaces() result, go in first loop. start getgroups(), stop , wait getgroups() result, go in sec loop. loops not concurrent have no need lock while adding them.

however if have noticed 2 async methods not concurrent, can modify programme create though.

public static async task<list<searchingitem>> getitemstoselect() { list<searchingitem> searchingitems = new list<searchingitem>(); var getplacestask = getplaces(); var getgroupstask = getgroups(); foreach (place place in await getplacestask) { searchingitems.add(new searchingitem() { idfromrealmodel=place.id, nametodisplay=place.fullname, extrainformation=place.name, typeofsearchingitem=typeofsearchingitem.place }); } foreach (group grouping in await getgroupstask) { searchingitems.add(new searchingitem() { idfromrealmodel = group.id, nametodisplay = group.name, extrainformation = group.typename, typeofsearchingitem = typeofsearchingitem.group }); } homecoming searchingitems; }

what start getplaces(), start getgroups(), stop , wait getplaces() result, go in first loop, stop , wait getgroups() result, go in sec loop.

the 2 loops still not concurrent, 2 await-able methods may give little performance boost. uncertainty benifit making loops concurrent, appear building models , overhead of making thread safe not worth how little work beingness done.

if wanted seek , create more parallel (but uncertainty see much benefit) utilize plinq build models.

public static async task<list<searchingitem>> getitemstoselect() { var getplacestask = getplaces(); var getgroupstask = getgroups(); var places = await getplacestask; //just create initial list linq object. list<searchingitem> searchingitems = places.asparallel().select(place=> new searchingitem() { idfromrealmodel=place.id, nametodisplay=place.fullname, extrainformation=place.name, typeofsearchingitem=typeofsearchingitem.place }).tolist(); var groups = await getgroupstask; //build plinq ienumerable var groupsearchitems = groups.asparallel().select(group=> new searchingitem() { idfromrealmodel = group.id, nametodisplay = group.name, extrainformation = group.typename, typeofsearchingitem = typeofsearchingitem.group }); //the building of ienumerable parallel adding serial. searchingitems.addrange(groupsearchitems); homecoming searchingitems; }

c# asynchronous multitasking async-await

No comments:

Post a Comment