当异步使用webclient时,抛出异常的第一次机会异常类型'System.NullReferenceException'

时间:2015-03-28 20:08:18

标签: c# asynchronous download webclient updates

所以我试图异步下载更新文件并抛出一个空异常:

A first chance exception of type 'System.NullReferenceException' occurred in OnePlus One Toolkit.exe
Additional information: Object reference not set to an instance of an object..

代码如下:

        try { webclient.DownloadFileAsync(new Uri("http://repo-itechy21.tk/toolkit/"), "update.msi"); }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }            
    webclient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(progress);
    webclient.DownloadFileCompleted += new AsyncCompletedEventHandler(Complete);

}
private void Complete(object sender, AsyncCompletedEventArgs e)
{
    if (e.Cancelled == true)
    {
        File.Delete("update.msi");
        MessageBox.Show("Download has been canceled.");
    }
    else
    {
        MessageBox.Show("Download completed! The Program will now exit and update");
        Process.Start("update.msi");
        Application.Exit();
    }
}

它通常会抛出这两个代码中的任何一个的异常:

webclient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(progress);
webclient.DownloadFileCompleted += new AsyncCompletedEventHandler(Complete);

我是c#的菜鸟,如果可能的话请用非专业术语解释。

0 个答案:

没有答案