简体   繁体   中英

Why when I click on the button to cancel the web client download progress I'm getting exception?

private void btnStop_Click(object sender, EventArgs e)
        {
            if (this.client != null)
                this.client.CancelAsync();
        }

In the completed event i have only a return; in the cancel:

private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                // handle error scenario
                throw e.Error;
            }
            if (e.Cancelled)
            {
                //client.Dispose(); // Method that disposes the client and unhooks events
                return;
                // handle cancelled scenario
            }

            if (url.Contains("animated") && url.Contains("infra"))
            {
                Image img = new Bitmap(lastDownloadedFile);
                Image[] frames = GetFramesFromAnimatedGIF(img);
                foreach (Image image in frames)
                {
                    countFrames++;
                    image.Save(downloadDirectory + "\\" + fname + ".gif");
                }
            }

            label2.Text = "Download Complete";

            string lastUrl = (string)e.UserState;

            listView1.BeginUpdate();
            foreach (ListViewItem li in listView1.Items)
            {
                if (li.SubItems[2].Text == lastUrl)
                {
                    li.SubItems[0].Text = "Downloaded";
                    li.SubItems.Add("Color");
                    li.SubItems[0].ForeColor = Color.Green;
                    li.UseItemStyleForSubItems = false;
                }
            }
            listView1.EndUpdate();

            tracker.NewFile();
            DownloadFile();
        }

The exception:

HResult=-2146233079 Message=The request was aborted: The request was canceled. Source=DownloadMultipleFiles StackTrace: at DownloadMultipleFiles.Form1.client_DownloadFileCompleted(Object sender, AsyncCompletedEventArgs e) in Form1.cs:line 193 at System.Net.WebClient.OnDownloadFileCompleted(AsyncCompletedEventArgs e) at System.Net.WebClient.DownloadFileOperationCompleted(Object arg) InnerException:

Line 193 is:

throw e.Error;

Inside the completed event.

e.Error = {"The request was aborted: The request was canceled."}

Should I do something in the completed event in the cancel part ? all id othere is a return.

It occurs that this.client.CancelAsync(); internally raises not only Cancelled flag but also sets Error to the exception you see. So the obvious way to fix your code is to swap two checks

private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    // First check for Cancelled and then for other exceptions
    if (e.Cancelled)
    {
        //client.Dispose(); // Method that disposes the client and unhooks events
        return;
        // handle cancelled scenario
    }
    if (e.Error != null)
    {
        // handle error scenario
        throw e.Error;
    }

    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM