WebClient.CancelAsync() and TargetInvocationException

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deerchao

    #1

    WebClient.CancelAsync() and TargetInvocationException

    I found a thread (http://groups.google.com/group/
    microsoft.publi c.dotnet.langua ges.csharp/browse_thread/thread/
    b154f44522b5e08 3/11974fe21507faa e?
    lnk=gst&q=webcl ient.CancelAsyn c&rnum=1#11974f e21507faae) about this
    issue, but it can't solve my problem.
    I simply put a progressbar (to show the progress of the downloading),
    a lable (to show what's downloading), and a button (to cancel the
    download) on a form. If I click cancel button while downloading, there
    comes a TargetInvocatio nException. How should I avoid it, or at least
    catch it?

    And here is the code:

    DownloadProgres sForm f=new DownloadProgres sForm(url, message)
    if(f.ShowDialgo ()==DialogResul t.OK)
    {
    ...
    }


    public partial class DownloadProgres sForm : Form
    {
    private readonly string url;
    private byte[] data;
    private WebClient client;

    public DownloadProgres sForm(string url, string message)
    {
    InitializeCompo nent();
    lblStatus.Text = message;
    this.url = url;
    }

    public byte[] Data
    {
    get { return data; }
    }

    private void DownloadProgres sForm_Load(obje ct sender,
    EventArgs e)
    {
    client = new WebClient();
    client.Download ProgressChanged += DownloadProgres sChanged;
    client.Download DataCompleted += DownloadDataCom pleted;
    client.Download DataAsync(new Uri(url));
    }

    void DownloadDataCom pleted(object sender,
    DownloadDataCom pletedEventArgs e)
    {
    if (e.Cancelled)
    {
    DialogResult =
    System.Windows. Forms.DialogRes ult.Cancel;
    }
    else
    {
    DialogResult = System.Windows. Forms.DialogRes ult.OK;
    }
    data = e.Result;
    Close();
    }

    void DownloadProgres sChanged(object sender,
    DownloadProgres sChangedEventAr gs e)
    {
    progressBar1.Va lue = (int)(e.BytesRe ceived * 100 /
    e.TotalBytesToR eceive);
    }

    private void btnCancel_Click (object sender, EventArgs e)
    {
    client.CancelAs ync();
    }
    }

  • deerchao

    #2
    Re: WebClient.Cance lAsync() and TargetInvocatio nException

    I don't know what's better than Application.Thr eadException event to
    catch this exception. But it works strangely: if in the event I pop a
    MessageBox, the application continues to run, If I don't, it will quit
    immediately! I believe it should be my decision to make whether or not
    exit the application, but not Application or whatever.
    What's wrong with my code?

    static void Main()
    {
    Application.Ena bleVisualStyles ();
    Application.Set CompatibleTextR enderingDefault (false);

    Application.Thr eadException +=
    Application_Thr eadException;
    Application.Run (new MainForm());
    }

    static void Application_Thr eadException(ob ject sender,
    ThreadException EventArgs e)
    {
    //logger.Error(e. Exception.Messa ge);
    //if the statement below is not present, application will
    exit at once!
    //but if it's here, application continues to run.
    MessageBox.Show (e.Exception.Me ssage);
    }

    Comment

    Working...