title: Status of Task getting stuck at "WaitingForActivation"
body: <p>This is in regard with .NET 4.0</p>
  
  <p>I am trying to fetch data from multiple WebAPIs asynchronously using Task. In console application, the program is working just fine but when I am creating a web application, the status of Task is always getting stuck at "WaitingForActivation". Any help would be highly appreciated. The following is my piece of code:</p>
  
  <pre><code>    public String[] CallClientAPI(string[] addresses)
      {
          CancellationTokenSource cts = new CancellationTokenSource();
          int timeToWait = 3000; //wait for 3 seconds to fetch API Data
  
          Task.Factory.StartNew(() =&gt;
          {
              Thread.Sleep(timeToWait);
              cts.Cancel();
          });
  
          Task&lt;String[]&gt; webTask = GetAPIData(addresses, cts.Token);
          //webTask is always in "WaitingForActivation" mode only ..
  
          if (!webTask.IsCompleted)
          {
              Thread.SpinWait(5000000);
          }
  
          String[] results = null;
  
          try
          {
              results = webTask.Result;
          }
          catch
          {               
          }
          //other code
      }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>--------------------Code for GetAPIData--------------------</p>
  
  <pre><code>    public Task&lt;string[]&gt; GetAPIData(String[] urls, CancellationToken token)
      {
          TaskCompletionSource&lt;string[]&gt; tcs = new TaskCompletionSource&lt;string[]&gt;();
          WebClient[] webClients = new WebClient[urls.Length];
  
          token.Register(() =&gt;
              {
                  foreach (var wc in webClients)
                  {
                      if (wc != null)
                          wc.CancelAsync();
                  }                    
              });
  
          object m_lock = new object();
          int count = 0;
          List&lt;string&gt; results = new List&lt;string&gt;();
          for (int i = 0; i &lt; urls.Length; i++)
          {
              webClients[i] = new WebClient();
  
              webClients[i].DownloadStringCompleted += (obj, args) =&gt;
                  {
                      if (args.Cancelled == true)
                      {
                          tcs.TrySetCanceled();
                          return;
                      }
                      else if (args.Error != null)
                      {
                          tcs.TrySetException(args.Error);
                          return;
                      }
                      else
                      {
                          results.Add(args.Result);
                      }
  
                      lock (m_lock)
                      {
                          count++;
                          if (count == urls.Length)
                          {
                              tcs.TrySetResult(results.ToArray());
                          }
                      }
                  };
  
              Uri address = null;
              try
              {
                  address = new Uri(urls[i]);
                  webClients[i].DownloadStringAsync(address, address);
              }
  
              catch (UriFormatException ex)
              {
                  tcs.TrySetException(ex);
                  return tcs.Task;
              }
          }
  
          return tcs.Task;
      }
  </code></pre>