title: Thread.Sleep for Google MAPS API
body: <p>I am using GoogleAPI to get the Driving Miles and Driving Distance. Here is my Source Code...</p>
  
  <pre><code> dsAPI.ReadXml("http://maps.googleapis.com/maps/api/directions/xml?origin=" + strBaseLat + "," + strBaseLong + "&amp;destination=" + strDStrLat + "," + strDStrLong + "&amp;sensor=false");
   Thread.Sleep(3000);
  </code></pre>
  
  <p>Edited Code :</p>
  
  <pre><code>dsAPI.ReadXml("http://maps.googleapis.com/maps/api/directions/xml?origin=" + strBaseLat + "," + strBaseLong + "&amp;destination=" + strDStrLat + "," + strDStrLong + "&amp;sensor=false");
              Thread.Sleep(3000);
              if (dsAPI.Tables[0].Rows.Count &gt; 0)
              {
                  status = dsAPI.Tables[0].Rows[0]["status"].ToString();
              }
              if (status == "OK")
              {
                  if (dsAPI.Tables[2].Rows.Count &gt; 0)
                  {
                      legid = Convert.ToInt32(dsAPI.Tables[2].Rows[0]["leg_Id"].ToString());
                  }
  
                  if (dsAPI.Tables[7].Rows.Count &gt; 0)
                  {
                      DataView dvMinutes = new DataView(dsAPI.Tables[7]);
                      dvMinutes.RowFilter = "leg_Id = '" + legid + "'";
                      decMinutes = Convert.ToDecimal(dvMinutes[0]["value"] as string);
                      Min = decMinutes / 60;
                      Min = decimal.Round(Min, 2, MidpointRounding.AwayFromZero);
  
                  }
                  if (dsAPI.Tables[8].Rows.Count &gt; 0)
                  {
                      DataView dvMiles = new DataView(dsAPI.Tables[8]);
                      dvMiles.RowFilter = "leg_Id = '" + legid + "'";
                      intMiles = Convert.ToInt32(dvMiles[0]["value"] as string);
                      Mil = Convert.ToDecimal(intMiles / 1609.34);
                      Mil = decimal.Round(Mil, 2, MidpointRounding.AwayFromZero);
                  }
  </code></pre>
  
  <p>Based on the latitude and longitude, the API will return the details. In the next line i had given Thread.sleep. </p>
  
  <p>if i dint give thread.sleep,whether the remaining part of the code will wait, till the service returns the result. I had checked in my code, sometimes the remaining part got executed without waiting for the result.</p>
  
  <p>Is there any alternative for this.</p>