1

你知道为什么我总是得到这个例外吗? 例外

这是我的代码

private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
    {
        toolStripProgressBar1.Maximum = (int)e.MaximumProgress;
        toolStripProgressBar1.Value = (int)e.CurrentProgress;
    }

我会很感激任何答案,谢谢。

4

5 回答 5

2

According to msdn:

The number of bytes that have been loaded or -1 to indicate that the download has completed.

And the -1 value is not valid progress value. And it indicates that download has completed so displaying progressbar is pointless. I think this state would be good place to hide progress window.

In your code you are casting form long to int which will cause you similar exception when you download file which size will be greater then 2,147,483,647 bytes (int.MaxValue). You should assign

toolStripProgressBar1.Maximum = 100;

and in event

toolStripProgressBar1.Value = (int)Math.Floor((e.CurrentProgress / (double)e.MaximumProgress) * 100);
于 2012-08-06T11:20:00.290 回答
1

看起来好像e.CurrentProgress正在返回 -1(已加载或下载已完成的字节数。)。您可以通过使用条件语句检查值是否 > -1 来停止发生的错误,如果是则更新进度条。

例如:

if ((int)e.CurrentProgress > -1) {
    toolStripProgressBar1.Value = (int)e.CurrentProgress;
}

http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowserprogresschangedeventargs.currentprogress.aspx

于 2012-08-06T11:20:11.050 回答
0

In your coding you are not checking the currentprogress value and simply assigning it to the progress bar, so the value when it is less than minimum value also been assigned to it. So, do it like below.

if((int)(e.CurrentProgress) > -1)
    toolStripProgressBar1.Value = (int)e.CurrentProgress; 
于 2012-08-06T11:19:29.193 回答
0

Your e.CurrentProgress is -1. Your minimum value is 0.

That's why you catch exception.

于 2012-08-06T11:19:42.400 回答
0

根据文档-下载完成时CurrentProgress,事件参数类的属性WebBrowserProgressChangedEventArgs值为 -1。

请使用以下代码确保进度条完全反映下载状态:

    private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
    {
        toolStripProgressBar1.Maximum = (int)e.MaximumProgress;
        toolStripProgressBar1.Value = ((int)e.CurrentProgress < 0 || (int)e.MaximumProgress < (int)e.CurrentProgress) ? (int)e.MaximumProgress : (int)e.CurrentProgress;
    }
于 2012-08-06T11:21:57.010 回答