c# - async ForEach for a big collection -


this collection contains 500 or 1000 elements. in cases there several collections. in ui thread works slowly. i'm making silverlight app async way make app fast. took easy approach(withuot using async operation or task.run() method). example of first approach:

foreach (specialsongs.songjson futuresongs in pop) {     specialsongs.songjson sooong = new specialsongs.songjson();     sooong.artist = futuresongs.artist;     sooong.duration = futuresongs.duration;     sooong.uri = futuresongs.uri;     sooong.title = futuresongs.title;     sooong.ownerid = futuresongs.ownerid;     sooong.id = futuresongs.id;     specialsongs.songjson songstocoll = new specialsongs.songjson();     songstocoll.artist = futuresongs.artist;     songstocoll.duration = futuresongs.duration;     songstocoll.genreid = futuresongs.genreid;     songstocoll.id = futuresongs.id;     songstocoll.lyricsid = futuresongs.lyricsid;     songstocoll.ownerid = futuresongs.ownerid;     songstocoll.title = futuresongs.title;     songstocoll.uri = futuresongs.uri;     songstocoll.song = sooong;     popbindlist.add(songstocoll); } poplonglist.itemssource = popbindlist; poptext.visibility = system.windows.visibility.collapsed; 

the second approach: (little async)

rocklonglist.itemssource = await task<list<specialsongs.songjson>>.run(() => {     list<specialsongs.songjson> callback = new list<specialsongs.songjson>();     foreach (specialsongs.songjson futuresongs in rock)     {         specialsongs.songjson sooong = new specialsongs.songjson();         sooong.artist = futuresongs.artist;         sooong.duration = futuresongs.duration;         sooong.uri = futuresongs.uri;         sooong.title = futuresongs.title;         sooong.ownerid = futuresongs.ownerid;         sooong.id = futuresongs.id;         specialsongs.songjson songstocoll = new specialsongs.songjson();         songstocoll.artist = futuresongs.artist;         songstocoll.duration = futuresongs.duration;         songstocoll.genreid = futuresongs.genreid;         songstocoll.id = futuresongs.id;         songstocoll.lyricsid = futuresongs.lyricsid;         songstocoll.ownerid = futuresongs.ownerid;         songstocoll.title = futuresongs.title;         songstocoll.uri = futuresongs.uri;         songstocoll.song = sooong;         callback.add(songstocoll);     }     return callback; }); rocktext.visibility = system.windows.visibility.collapsed; 

i'm new making loops using async. found example:http://blogs.msdn.com/b/pfxteam/archive/2012/03/04/10277325.aspx ,but don't know how use. thanks, sorry english , formalization. hope decisions solve problem.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -