c# - Multiple Request Parallel and Observable collection -
how add task observablecollection? anytime loops replaces items in collection. want loop , store 4 items i.e. 8 items in total. loop again , add 4 items instead of replacing first items. supposed return photos if effort because of implicit conversion conflict between int , observable collection. ps. still maintain multiple request in parallel
private async task<int> processurl(string url, httpclient client) { httpresponsemessage response = await client.getasync(url); var responsecontent = await response.content.readasstringasync(); htmldocument htmldocument = new htmldocument(); htmldocument.loadhtml(responsecontent); observablecollection<photo> photo = new observablecollection<movie>(); foreach (var div in htmldocument.documentnode.descendants().where(i => i.name == "div" && i.getattributevalue("class", "").startswith("ml-item"))) { string image = div.descendants().where(i => i.name == "img").firstordefault().getattributevalue("data-original", ""); photo.add(new photo() { image = image }); } return something; }
as far understood question problem(assumed example) obesrvablecollection created each time call process url. able store photos parallel requests need move obesrvablecollection out of method , make private field or property. avoid sync issues accessing collection parallel requests need use concurrent observablecollection - example of can find @ following topic: concurrent observable collection, dictionary, , sorted dictionary
just remove observablecollection<photo> photo = new observablecollection<movie>();
method , add parsed photos class-scope property or field concurrentobservablecollection type.
so first need download sources article , concurrentobservablecollection class there , add project. change type: observablecollection concurrentobservablecollection. inside processurl remove observablecollection photo = new observablecollection(); , in end change photo.add(new photo() { image = image }); _photo.add(new photo() { image = image }); - using property. not forget clean collection if try use collection once more after parrallel requests finished.
Comments
Post a Comment