1

Here's my code to download the file.

WebClient client=new WebClient()
    public void DownloadFile()
    {
                Uri uri = new Uri(url);
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged);

                client.DownloadFileAsync(uri, fileName);

    }

Here's my code to cancel the downloading file.

public void CancelDownloadingFile()
{
      client.CancelAsync();
      client.Dispose();
}

But when i call the CancelDownloadingFile() method it does nothing. The File keeps downloading in the background. Any suggestions is appreciated. Thanks.

Varadha31590
  • 371
  • 5
  • 20

1 Answers1

-2

You can achieve this using CancellationToken

CancellationTokenSource cts; // write this on top of your class

public void CancelDownloadingFile()
{
      if (cts != null)
    {
        cts.Cancel();
    }
}

Source : https://msdn.microsoft.com/en-IN/library/jj155759.aspx

possible duplicate : How to cancel an asynchronous call?

Community
  • 1
  • 1
Bhavik Patel
  • 1,466
  • 1
  • 12
  • 28
  • Downvoted because this doesn't actually answer OP's question, which is how to cancel a WebRequest.DownloadFileAsync call. – Walt D Nov 12 '19 at 19:00