title: Unable to debug a program that uses concurrent async / await
body: <p><strong>Observations <a href="http://code.msdn.microsoft.com/Async-Sample-Example-from-9b9f505c" rel="nofollow noreferrer">on this program</a>:</strong></p>
  
  <ul>
  <li><p>Pressing F11 slowly through this program doesn't show every execution of <code>ProcessURL()</code></p></li>
  <li><p>Pressing F11 quickly through this program shows more executions of <code>ProcessURL()</code></p></li>
  <li><p>Using <code>Thread.Sleep(3000);</code> in ProcessURL results in the MainUI thread hanging for approximately 30 seconds.  No UI Redraws, and the cancel button is unavailable.</p></li>
  </ul>
  
  <p><strong>Needs:</strong></p>
  
  <ul>
  <li>I want to step through every execution of ProcessURL, or perhaps visualize it like this using native Visual Studio tooling or open source add ins</li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/cJ9o6.png" alt="enter image description here"></p>
  
  <p><strong>Code</strong></p>
  
  <p><a href="http://code.msdn.microsoft.com/Async-Sample-Example-from-9b9f505c" rel="nofollow noreferrer">Download available here</a></p>
  
  <pre><code>namespace ProcessTasksAsTheyFinish
  {
      public partial class MainWindow : Window
      {
          // Declare a System.Threading.CancellationTokenSource.
          CancellationTokenSource cts;
  
          public MainWindow()
          {
              InitializeComponent();
          }
  
          private async void startButton_Click(object sender, RoutedEventArgs e)
          {
              resultsTextBox.Clear();
  
              // Instantiate the CancellationTokenSource.
              cts = new CancellationTokenSource();
  
              try
              {
                  await AccessTheWebAsync(cts.Token);
                  resultsTextBox.Text += "\r\nDownloads complete.";
              }
              catch (OperationCanceledException)
              {
                  resultsTextBox.Text += "\r\nDownloads canceled.\r\n";
              }
              catch (Exception)
              {
                  resultsTextBox.Text += "\r\nDownloads failed.\r\n";
              }
  
              cts = null;
          }
  
  
          private void cancelButton_Click(object sender, RoutedEventArgs e)
          {
              if (cts != null)
              {
                  cts.Cancel();
              }
          }
  
  
          async Task AccessTheWebAsync(CancellationToken ct)
          {
              HttpClient client = new HttpClient();
  
              // Make a list of web addresses.
              List&lt;string&gt; urlList = SetUpURLList();
  
              // ***Create a query that, when executed, returns a collection of tasks.
              IEnumerable&lt;Task&lt;int&gt;&gt; downloadTasksQuery =
                  from url in urlList select ProcessURL(url, client, ct);
  
              // ***Use ToList to execute the query and start the tasks. 
              List&lt;Task&lt;int&gt;&gt; downloadTasks = downloadTasksQuery.ToList();
  
              // ***Add a loop to process the tasks one at a time until none remain.
              while (downloadTasks.Count &gt; 0)
              {
                      // Identify the first task that completes.
                      Task&lt;int&gt; firstFinishedTask = await Task.WhenAny(downloadTasks);
  
                      // ***Remove the selected task from the list so that you don't
                      // process it more than once.
                      downloadTasks.Remove(firstFinishedTask);
  
                      // Await the completed task.
                      int length = await firstFinishedTask;
                      resultsTextBox.Text += String.Format
                          ("\r\nLength of the download:  {0}", length);
              }
          }
  
  
          private List&lt;string&gt; SetUpURLList()
          {
              List&lt;string&gt; urls = new List&lt;string&gt; 
              { 
                  "http://msdn.microsoft.com",
                  "http://msdn.microsoft.com/library/windows/apps/br211380.aspx",
                  "http://msdn.microsoft.com/en-us/library/hh290136.aspx",
                  "http://msdn.microsoft.com/en-us/library/dd470362.aspx",
                  "http://msdn.microsoft.com/en-us/library/aa578028.aspx",
                  "http://msdn.microsoft.com/en-us/library/ms404677.aspx",
                  "http://msdn.microsoft.com/en-us/library/ff730837.aspx"
              };
              return urls;
          }
  
  
          async Task&lt;int&gt; ProcessURL(string url, HttpClient client, CancellationToken ct)
          {
              // GetAsync returns a Task&lt;HttpResponseMessage&gt;. 
              HttpResponseMessage response = await client.GetAsync(url, ct);
              // Retrieve the website contents from the HttpResponseMessage.
              byte[] urlContents = await response.Content.ReadAsByteArrayAsync();
  
              return urlContents.Length;
          }
      }
  }
  </code></pre>