I have the following method in my application
public async void Method()
{
bool updated = false;
foreach (Feed feed in Feeds)
{
if (await feed.Update())
{
updated = true; // At least one feed was updated
}
}
if (updated)
{
// Do Something
}
}
As you can see, the method Update()
is called one by one, on each item in the list. I would instead like to call it on all the items simultaneously, and know whether one of them succeeded.
The method Update()
is found in the class Feed
public async Task<bool> Update()
{
try
{
WebRequest wr = WebRequest.Create(URL);
wr.Timeout = 5000;
using (WebResponse response = await wr.GetResponseAsync())
{
XmlDocument feed = new XmlDocument();
feed.Load(response.GetResponseStream());
// Do Something
return true;
}
}
catch (WebException we)
{
return false;
}
}
EDIT:
So far I've been trying to solve this problem using async methods with and without return values.
public async void Update()
{
if (await UpdateFeeds())
{
// Do something
}
}
public async Task<bool> UpdateAllFeeds()
{
// Update all the feeds and return bool
}
Then I realized I would still have the same problem within UpdateAllFeed()
. They could run simultaneously if I changed Update()
in Feed to an async void method, but then I would have no callback.
I don't know how to run multiple asynchronous methods and only callback when they're all done.