private void ExecuteWebRequest(string url, Action<string> callback, Action<Exception> error)
{
DispatcherTimer timer = new DispatcherTimer();
// create a web client to fetch the URL results
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += (s, e) =>
{
timer.Stop();
try
{
string result = e.Result;
callback(result);
}
catch (Exception ex)
{
error(ex);
}
};
// initiate the download
webClient.DownloadStringAsync(new Uri(url));
// create a timeout timer
timer.Interval = TimeSpan.FromSeconds(5);
timer.Start();
timer.Tick += (s, e) =>
{
timer.Stop();
webClient.CancelAsync();
error(new TimeoutException());
};
}