Reputation: 43
Description: I have multiple successive image downloads and saving in IsolatedStorage using HttpWebRequest. After all image downloadsa are completed I need to navigate user to another page, where images are displayed in image controls from isolated storage.
Question: How can I know when all the downloads are completed to run the navigation?
I tried to pass the redirect to the requests callback function (requestImage_BeginGetResponse()) in the last foreach loops iteration after saving the image, but the images are different sizes and sometimes the last image downloads faster than previous, that results in redirect before all downloads are completed.
the code:
private HttpWebRequest request;
private void downloadDataFile()
{
...
foreach (Gallery image in gallery)
{
request = (HttpWebRequest)WebRequest.Create(image.url);
request.BeginGetResponse(new AsyncCallback(requestImage_BeginGetResponse), new object[] { request, image.name });
}
}, request);
}
private void requestImage_BeginGetResponse(IAsyncResult r)
{
object[] param = (object[])r.AsyncState;
HttpWebRequest httpRequest = (HttpWebRequest)param[0];
string filename = (string)param[1];
HttpWebResponse httpResoponse = (HttpWebResponse)httpRequest.EndGetResponse(r);
System.Net.HttpStatusCode status = httpResoponse.StatusCode;
if (status == System.Net.HttpStatusCode.OK)
{
Stream str = httpResoponse.GetResponseStream();
Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
{
saveImage(str, filename);
}));
}
}
Upvotes: 2
Views: 319
Reputation: 63
You should prepare a int type variable to record your images that would be downloaded.Whenever a image is downloaded,make the variable minus 1 untill its value is 0,and notify the navigating operation.
Upvotes: 1