title: Process batch request callback asynchronous in .NET 4 and WebForms
body: <p>In the example blow you can see I'm executing a batch request on a button click. After that I need to use the information provided in the callback, but without freezing the WebForms page. I have thought that the callback is asynchronous by itself, but obviously I'm wrong, because until the callback is not processed the page stays frozen.</p>
  
  <pre><code>batchRequest.Queue&lt;Google.Apis.Calendar.v3.Data.Event&gt;(
      addRequest,
      (content, error, i, message) =&gt;    // callback
      {
          using (dbContext)
          {
              Event eventToUpdate = dbContextNewInstance.Events.FirstOrDefault(x =&gt; x.Id == dbObj.Id);
              if (eventToUpdate != null)
              {
                  eventToUpdate.GoogleCalendarMappingId = content.Id;
                  dbContextNewInstance.SubmitChanges();
              }
          }
      });
  
  batchRequest.ExecuteAsync();
  </code></pre>
  
  <p>*UPDATE:
  I have made this implementation and its worked! So far I am worried is it everything going the correct way and no thread or DB connection is left unmanaged, guys?</p>
  
  <pre><code>batchRequest.Queue&lt;Google.Apis.Calendar.v3.Data.Event&gt;(
                                   addRequest,
                                   (content, error, i, message) =&gt;    // callback
                                   {
                                       idsToMap[dbObj.Id] = content.Id; // A dictionary for my dbObj Id and the Id I receive from the remote API in the CALLBACK
                                   });
  
  
                  Thread batchThread = new Thread(() =&gt; SubmitBatchRequest(batchRequest, idsToMap, connectionString));
              batchThread.Start();
  </code></pre>
  
  <p>And the method with the Thread:</p>
  
  <pre><code>private static void SubmitBatchRequest(BatchRequest batchRequest, Dictionary&lt;Guid, string&gt; ids, string connectionString)
  {
      Thread.CurrentThread.IsBackground = true;
  
      batchRequest.ExecuteAsync().GetAwaiter().GetResult(); // Send the batch request asynchronous
  
      using (DataContext db = new DataContext(connectionString))
      {
          foreach (Guid dbObjId in ids.Keys)
          {
              Event eventToUpdate = db.Events.FirstOrDefault(x =&gt; x.Id == dbObjId);
              if (eventToUpdate != null)
              {
                  eventToUpdate.GoogleCalendarMappingId = ids[dbObjId];
              }
          }
  
          // Thread.Sleep(50000);
          db.SubmitChanges();
      }
  }
  </code></pre>