c# - Async method failing when called -
i have method
public class websearcher : iwebsearcher { private static readonly string _rooturi; private static readonly bingsearchcontainer _bingcontainer; private static readonly string _accountkey; static websearcher() { _rooturi = configurationsettings.settings.rooturi; _bingcontainer = new bingsearchcontainer(new uri(_rooturi)); _accountkey = configurationsettings.settings.accountkey; _bingcontainer.credentials = new networkcredential(_accountkey, _accountkey); } public task<ienumerable<webresult>> searchasynch(string query) { if (query == null) { throw new argumentnullexception("query cannot null"); } dataservicequery<webresult> webquery = _bingcontainer.web(query, null, null, null, null, null, null, null); return task.factory.fromasync(webquery.beginexecute(null, null), asyncresult => webquery.endexecute(asyncresult)); } }
and call this
public class client { public static void main() { search(); } private static async task search() { var tasks = new task<ienumerable<webresult>>[100]; iwebsearcher websearcher = new websearcher(); (var = 0; < 100; i++) { tasks[i] = websearcher.searchasynch(i.tostring()); } await task.whenall(tasks); (var = 0; < 100; i++) { console.writeline(tasks[i].result.first().title); } } }
the code stops executing @ whenall line.
you need change search async void async task , wait() result in main - otherwise, exits after task starts, nothing waiting finish.
Comments
Post a Comment