title: async SMS from asp.net using twilio
body: <p>I have an asp.net web application that notifies business clients about available appointments. 
  The scenario is as below. </p>
  
  <ol>
  <li>Page button click event handler saves new appointment to the database </li>
  <li>Preferences of registered clients are matched to the new appointment created </li>
  <li>Clients whose preferences match are sent an SMS message through Twilio with the appointment details. </li>
  </ol>
  
  <p>My problem is that I need to separate steps 2 &amp; 3 from the page's execution. So once an appointment is created the following steps are passed on asynchronously to a helper class which handles the matching and SMS messaging on a background thread, while the page control returns and the user is redirected to another page. The background class method doesn't return any value (public void).</p>
  
  <p>Sorry for not posting the code earlier.</p>
  
  <p>createappointment.aspx.cs</p>
  
  <pre><code>  private void SaveAppointment()
  {
      using (var db = new EntitiesModel())
      {
         //code to create a new appointment
  
       //Sending SMS messages to multiple clients
  
          SMSHelper myhelper = new SMSHelper();
          myhelper.SendApptSMS(newappt);
  
          //control returned to the page redirect to dashboard
          Response.Redirect("~/authuser/default.aspx");
      }
  }
  </code></pre>
  
  <p>smshelper.cs</p>
  
  <pre><code>public async void SendApptSMS(Appointment newappointment)
  {
      using (var db = new EntitiesModel())
      {
          //retrieve multiple clients that match preferences
      foreach (var item in clients)
     {
        //for each client send SMS message using Twilio's REST API
      }
          //Thread.Sleep(10000);
  
      }
  }
  </code></pre>
  
  <p>I expect page to continue processing once the call to the async method has been made, which then executes on a background thread.
  This doesn't happen.
  The response.redirect is not reached until the SMSHelper has finished sending out all the messages.</p>
  
  <p>Hope this clears up the problem.</p>