c# - Async tasks stop running when one of them returns false -


i have 2 methods return type task<bool>, isorderworking() , iswebpageworking().

i run these 2 methods whenall, this:

var siteworking = await task.whenall(isorderworking(), iswebpageworking());  if (siteworking[0] == true && siteworking[1] == true) {     return true; } else  {     return false; } 

thing is, seem run sequentially after another, meaning if iscartworking() returns false after operations in isorderworking() have been executed.

how make these 2 tasks run asynchronously? want methods exit , return false hit it, instead of running after another.

edit: both these methods have same approach, 1 of them:

    public static async task<bool> iswebpageworking()     {         using (var client = new httpclient())         {             var response = await client.postasync("url/that/makes/a/post/to/cart", null);             if (response.statuscode == httpstatuscode.ok)             {                 return true;             }             else             {                 return false;             }         }     } 

the methods running asynchronously , concurrently.

i want methods exit , return false hit it, instead of running after another.

they are. think meant ask want calling method return false any of tasks return false.

to this, can use task.whenany rather task.whenall:

var tasks = new list<task<bool>> { isorderworking(), iswebpageworking() }; while (tasks.count > 0) {   var completedtask = await task.whenany(tasks);   if (!await completedtask)     return false;   tasks.remove(completedtask); } return true; 

this simplistic algorithm doesn't scale massive numbers of tasks, fine few tasks. if need scale this, consider using this stephen toub or this jon skeet.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -