title: How to resolve Tasks hanging c#?
body: <p>While going over some code in a console application, I saw the nested Task.WhenAll in the <code>SecondInitialize</code> function. I decided to test this function with a large Locations List and see how it reacted.</p>
  
  <p>What I saw is that with about 100 locations, 100*100 = 10,000 <code>Calculate</code> calls, the <code>t.Wait()</code> inside of <code>Start</code> takes about 60 seconds to return or sometimes just hang completely. If i try to click <code>Break All</code> the console application doesn't even respond to my click and visual studio crashes.</p>
  
  <p>When using my 'Easier to read version' inside of <code>SecondInitialize</code>, it also takes a while to return. Consistent behavior.</p>
  
  <p>Now the weird part is, whenever I use the debugger and put a breakpoint inside of the <code>SecondInitialize</code> and then hit continue, it will finish in about 5-7 seconds. </p>
  
  <p>So my question is, why is it hanging taking a long time normally when I see it being faster when I debug inside that function? Another question is whether or not the use of <code>Tasks</code> is being utilized correctly</p>
  
  <pre><code>public void Start()
  {
      var t = CacheInitialize(locations, CancellationToken.None);
      t.Wait();
  }
  
  public Task CacheInitialize(IList&lt;Location&gt; locations, CancellationToken token)
  {
      return SecondInitialize(locations, token);
  }
  
  public async Task SecondInitialize(IList&lt;Location&gt; locations, CancellationToken token)
  {
      await Task.WhenAll(locations.Select(first =&gt;
      {
          return Task.WhenAll(locations.Where(second =&gt; !second.Equals(first)).Select(second =&gt;
          {
              return Calculate(first, second, token);
          }));
      }));
  
      //Easier to read version of ^
      //var tasks = locations.SelectMany(first =&gt; locations.Where(second =&gt; !second.Equals(first)).Select(second =&gt;
      //{
      //  return Calculate(first, second, token);
      //}));
      //await Task.WhenAll(tasks);
  
  
      //No Tasks.
      //for (int x = 0; x &lt; locations.Length; x++)
      //{
      //    for (int y = 0; y &lt; locations.Length; y++)
      //    {
      //        if (x == y)
      //            continue;
      //        await Calculate(locations[x], locations[y], token).ConfigureAwait(false);
      //    }
      //}
  }
  
  public async Task&lt;TripLength&gt; Calculate(Location start, Location finish, CancellationToken token)
  {
      if (start == finish)
          return TripLength.Zero;
  
      var parameters = new RouteParameters
      {
          Coordinates = new []
          {
              new Coordinate(start.Latitude, start.Longitude),
              new Coordinate(finish.Latitude, finish.Longitude)
          }
      };
  
      var route = await RunRoute(parameters, token);
  
      return ToTripLength(route);
  }
  
  
  protected Task&lt;RouteResult&gt; RunRoute(RouteParameters routeParams, CancellationToken token)
  {
      return Task.Run(async () =&gt;
      {
          var routingTask = Task.Run(() =&gt;
          {
              RouteResult routeResults;
              var status = _routeService.Route(routeParams, out routeResults);
              return routeResults;
          }, token);
      return await routingTask.ConfigureAwait(false);
  
      }, token);
  }
  </code></pre>