c# - Async methods not running -
this question has answer here:
i'm trying hang of async-await methods i'm not getting hang of it. used webrequest, simple sadly synchronous.
my problem starttest() not running -- "because call not awaited, execution of current method continues before call completed. consider applying 'await' operator result of call'. can't add await before parent method main, , can't of course async method. realize understanding of async horrible -- point me in right way?
static void main(string[] args) { starttest(); async task<string> starttest() { using (var client = new httpclient()) { list<string> urls = new list<string> { //a list of urls want check }; if (await getstatuscodes(urls) != true) { return "not ok"; } } return "ok"; } async task<bool> getstatuscodes(ilist<string> urls) { using (var client = new httpclient()) { foreach (var url in urls) { var response = await client.getasync(url); if (response.statuscode.tostring() != "ok") { return false; } } return true; } } }
you can make mainasync method put stuff in want run async.
e.g.
private static void main() { mainasync().wait(); } private static async task mainasync() { // async stuff }
Comments
Post a Comment