I am implementing a mobile app using Xamarin android. I have implemented a code to download both.PDF and .Mobi file in a button click. I have used the below code.
...
await Task.WhenAll(DownloadPDF(), DownloadMobi());
}
private async Task DownloadPDF()
{
var httpclient = new HttpClient(new AndroidClientHandler());
using (var stream = await httpclient.GetStreamAsync("http://files/file.pdf"))
using (var file = System.IO.File.Create("path/to/file.pdf"))
{
await stream.CopyToAsync(file);
await file.FlushAsync();
}
}
private async Task DownloadMobi()
{
var httpclient = new HttpClient(new AndroidClientHandler());
using (var stream = await httpclient.GetStreamAsync("http://files/file.mobi"))
using (var file = System.IO.File.Create("path/to/file.mobi"))
{
await stream.CopyToAsync(file);
await file.FlushAsync();
}
}
Its download both file at a same time. I want to download the PDF file at first. Once PDF file has been downloaded the button text should be changed to "View PDF" from "Download". When click View PDF the the file should be opened in PDF reader. The Mobile file download should start after this process and download should be in background. Can you anyone suggest your ideas to achieve this?
Since you're not showing any UI stuff i guess you have that covered to i will omit that.
Instead of writing:
await Task.WhenAll(DownloadPDF(), DownloadMobi());
do the following
Use ContinueWith Method of
Task
This will continue execution of next task after pdf download task is complete