title: Task Parallel Library WaitAny with specified result
body: <p>I'm trying to write some code that will make a web service call to a number of different servers in parallel, so TPL seems like the obvious choice to use.</p>
  
  <p>Only one of my web service calls will ever return the result I want and all the others won't. I'm trying to work out a way of effectively having a <code>Task.WaitAny</code> but only unblocking when the first <code>Task</code> that matches a condition returns.</p>
  
  <p>I tried with <code>WaitAny</code> but couldn't work out where to put the filter. I got this far:</p>
  
  <pre><code>public void SearchServers()
  {
      var servers = new[] {"server1", "server2", "server3", "server4"};
      var tasks = servers
                   .Select(s =&gt; Task&lt;bool&gt;.Factory.StartNew(server =&gt; CallServer((string)server), s))
                   .ToArray();
  
      Task.WaitAny(tasks); //how do I say "WaitAny where the result is true"?
  
      //Omitted: cancel any outstanding tasks since the correct server has been found
  }
  
  private bool CallServer(string server)
  {
      //... make the call to the server and return the result ...
  }
  </code></pre>
  
  <p><strong>Edit</strong>: Quick clarification just in case there's any confusion above. I'm trying to do the following:</p>
  
  <ol>
  <li>For each server, start a <code>Task</code> to check it</li>
  <li>Either, wait until a server returns true (only a max of 1 server will ever return true)</li>
  <li>Or, wait until all servers have returned false, i.e. there was no match.</li>
  </ol>