Skip to content

Instantly share code, notes, and snippets.

@afrikaan-official
Last active October 18, 2016 07:11
Show Gist options
  • Save afrikaan-official/9b405371417cbf0c30fa6564d6e0969e to your computer and use it in GitHub Desktop.
Save afrikaan-official/9b405371417cbf0c30fa6564d6e0969e to your computer and use it in GitHub Desktop.
csharp paralel requests.
public static async Task<string> CalculateDistance(string origins, string destination)
{
string url = string.Format(urlBase, origins, destination);
RestClient client = new RestClient(url);
var request = new RestRequest();
var response = await client.ExecuteGetTaskAsync(request);
DistanceModel model = JsonConvert.DeserializeObject<DistanceModel>(response.Content);
var firstOrDefault = model.Rows.FirstOrDefault();
Elements orDefault = firstOrDefault?.Elements.FirstOrDefault();
if (orDefault != null)
return orDefault.Duration.Text;
return string.Empty;
}
public async Task<HttpResponseMessage> CalculateParallel(DistanceCalculationModel model)
{
HttpResponseMessage message;
var allTasks = from station in model.Stations
select DistanceMatrixCalculator.CalculateDistance(model.Origin,station.Lat+","+station.Lng);
string[] results = await Task.WhenAll(allTasks);
message = Request.CreateResponse(HttpStatusCode.OK, results);
return message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment