You can write a single WebClient class and you can call it in a loop with different async request. As,
WebClient client = new WebClient();
try
{
client.DownloadStringCompleted += (object newSender, DownloadStringCompletedEventArgs e) =>
{
Dispatcher.BeginInvoke(() =>
{
try
{
var response = e.Result;
// your response logic.
}
catch (Exception)
{
MessageBox.Show("Problem occured");
}
});
};
}
catch
{
MessageBox.Show("Problem occured");
}
finally
{
if (userHasCanceled)
client.DownloadStringAsync(new Uri("xyz"));
}
client.DownloadStringAsync(new Uri("abc"));
So when you call client.CancelAsyn()
it might throw an exception
, which is handled in try-catch block and at last you can call a new async request in finally block. You can also put check in finally block to confirm whether user has canceled
the operation, if yes
then call new async request else
do nothing.
I hope this is what you were looking for.