Q:

Asynchronous coding - waiting for all returns - performance

I have code similar to the following pseudo code:
static int counter;

function GetCalculations()
{
     for (x service calls)
     {
          service.BeginCalc(MyCallback);

          Interlocked.Increment(counter);
     }

     while(counter > 0)
     {
         //Wait for all results to return
     }

     return;
}

static function MyCallback()
{
   try
   {
   ... process results
   }
   finally
   {
      Interlocked.Decrement(counter);
   }
}

My question is in relation to the wait in the above code (the while (counter > 0)).  Is this going to be a performance issue?  I know that the multiple calls that I am making (to remote web services) take at least a few seconds to return - would I be better introducing something like a Thread.Sleep() in the while loop so that I am only checking for all returned every quarter second or so?
My gut instinct is that putting thread sleeps into my code smells a bit, but I'm just not well enough versed in this kind of code to say one way or the other.

A:

You could use task parallel library. It has easy to use mechanisms to wait on Tasks
Below is an example from MSDN
    Task[] tasks = new Task[3] 
    {
        Task.Factory.StartNew(() => MethodA()),
        Task.Factory.StartNew(() => MethodB()),
        Task.Factory.StartNew(() => MethodC())
    };

//Block until all tasks complete.
Task.WaitAll(tasks);

