I need to download a pdf file and save in device. I have used WebClient process to download a file and show progress while downloading it.
CancellationTokenSource Token= new CancellationTokenSource(); //Initialize a token while start download
webClient.DownloadFileTaskAsync(new Uri(downloadurl), saveLocation); // Download file
Download is working properly. To cancel the download which is in progress, I have used cancellationtokensource as mentioned in below link.
https://learn.microsoft.com/en-us/dotnet/standard/threading/cancellation-in-managed-threads
Token.Cancel(); //Cancellation download
try
{
// check whether download cancelled or not
Token.ThrowIfCancellationRequested();
if(Token.IsCancellationRequested)
{
//Changed button visibility
}
}
catch (OperationCanceledException ex)
{
}
It takes more seconds to cancel the download. Can you please suggest me to reduce the delay in cancelling download?