title: Exception while running System.Threading.Tasks.Task
body: <p>Consider the following code which uses basic Task library functionality with a CancellationTokenSource. It starts up a thread which fills a Dictionary with prices and reads the data from an SQL server db. The thread ends after about 10 minutes and every 2 hours it is fired up again, calling Cancel first in case the thread was still running.</p>
  
  <pre><code>private CancellationTokenSource mTokenSource = new CancellationTokenSource();
  
  internal Prices(Dictionary&lt;string, Dealer&gt; dealers)
  {
      mDealers = dealers;
      mTask = Task.Factory.StartNew
              (() =&gt; ReadPrices(mTokenSource.Token), mTokenSource.Token);
  }
  
  internal void Cancel() 
  {
      mTokenSource.Cancel();
  }
  private void ReadPrices(CancellationToken ct) 
  {
       using (SqlConnection connection = 
              new   SqlConnection(ConfigurationManager.AppSettings["DB"]))   
       {
           connection.Open();
           var dealerIds = from dealer in mDealers.Values 
                           where dealer.Id != null 
                           select dealer.Id;
           foreach (var dealerId in dealerIds) 
           {
               if (!ct.IsCancellationRequested)
               {
                   FillPrices(connection);
               }
               else
                  break;
          }
      }
  }
  </code></pre>
  
  <p>Now at some point the application crashes with the following exception in the event log.</p>
  
  <blockquote>
    <p>Application: Engine.exe Framework Version: v4.0.30319 Description: The
    process was terminated due to an unhandled exception. Exception Info:
    System.AggregateException Stack:    at
    System.Threading.Tasks.TaskExceptionHolder.Finalize()</p>
  </blockquote>
  
  <p>It must have to do with the code here because the Tasks library isn't used anywhere else but I cant figure out what is wrong with the code. Does anyone have an idea what could be wrong here?</p>