c# - Resolve two Async Code Samples -
i need return dictionary<string, byte[]> list of urls.
i have 1 code sample executing asynchronously, returns list<byte[]>, , seems casting object, throwing threading errors.
//seems assign output type correctly, doesn't execute async task<dictionary<string, byte[]>> returnfiledata(ienumerable<string> urls) { dictionary<uri, task<byte[]>> dictionary; using (var client = new webclient()) { dictionary = urls.select(url => new uri(url)).todictionary( uri => uri, client.downloaddatataskasync); await task.whenall(dictionary.values); } return dictionary.todictionary(pair => path.getfilename(pair.key.tostring()), pair => pair.value.result); } //executes properly, isn't returning right output type async task<list<byte[]>> returnfiledata2(ienumerable<string> urls) { dictionary<uri, task<byte[]>> dictionary; var tasks = urls.select(uri => new webclient().downloaddatataskasync(uri)); var results = await task.whenall(tasks); return results.select(result => result).tolist(); } how take returnfiledata2() , fix cast filename of uri alongside returned byte[] in output?
you should stick returnfiledata rework use separate client each url:
async task<dictionary<string, byte[]>> returnfiledata(ienumerable<string> urls) { dictionary<uri, task<byte[]>> dictionary = urls .select(url => new uri(url)) .todictionary(uri => uri, getthedataasync); await task.whenall(dictionary.values); return dictionary .todictionary( pair => path.getfilename(pair.key.tostring()), pair => pair.value.result); } async task<byte[]> getthedataasync(uri uri) { using (var client = new webclient()) { return await client.downloaddatataskasync(uri); } }
Comments
Post a Comment