简体   繁体   English

C#进度栏-BackgroundWorker进度的错误已更改

[英]C# Progress bar - error on BackgroundWorker's progress changed

What it does : 它的作用是

Reads the values in a datagridview into the database ACTB row by row. 逐行将datagridview中的值读取到数据库ACTB中。


To be clear, this exact same code works on my other program without issue, I don't know why my current program is throwing errors every time I run the background worker. 要清楚,这个完全相同的代码可以在我的其他程序上正常工作,我不知道为什么我每次运行后台工作程序时当前程序都会引发错误。

I receive the error: 2500 is not a valid value for 'Value' (progressbar.value) 'Value' must be between minimum and maximum. 我收到错误消息:2500不是'Value'(progressbar.value)的有效值'Value'必须介于最小值和最大值之间。

Here is my code. 这是我的代码。

Do Work : 做事

    int MaxSchema = 0;
    private void Transfer_Worker_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            using (OleDbConnection conn = new OleDbConnection(Con))
            {
                using (OleDbCommand cmd = new OleDbCommand())
                {
                    cmd.Connection = conn;
                    conn.Open();
                    MaxSchema = DGVExcel.Rows.Count;
                    for (int s = 0; s < DGVExcel.Rows.Count; s++) 
                    {
                    Transfer_Worker.ReportProgress(s);
                    }
                }
            }
        }
        catch (OleDbException ex)
        {
            MessageBox.Show("Import error: " + ex);
        }
        Transfer_Worker.ReportProgress(100);
    }

Progress Changed : 进度已更改

 private void Transfer_Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        pbar.Value = (e.ProgressPercentage * 100) / MaxSchema;
    }

Now, commenting out my code in progress changed seems to make the process run as expected, even the progressbar's values get updated. 现在,注释掉我正在进行的代码已更改,似乎可以使流程按预期运行,即使进度条的值也得到更新。 I don't know why this is, if the same block of code works elsewhere. 我不知道这是为什么,如果相同的代码块在其他地方也可以工作。

I have not modified the progressbar in anyway, so it should still have default properties. 无论如何我都没有修改进度条,因此它仍应具有默认属性。

Could it be that it is because I report progress on my dowork function? 可能是因为我报告了工作功能的进度吗? Correct me if this is something I need to change. 如果这是我需要更改的话,请纠正我。 First of all, I assume that I am not permitted to update the UI with the Work method of Background workers, so it should throw an error but it doesn't. 首先,我假定我不允许使用Background worker的Work方法更新UI,因此它应该引发错误,但不会。

EDIT : It appears that the error is caused by the line 编辑 :似乎该错误是由该行引起的

  Transfer_Worker.ReportProgress(100);

Changing the value from 100 (Presumed maximum) to MaxSchema (Actual value of rows.Count), the progress bar was able to correctly determine the end point. 将值从100(假定的最大值)更改为MaxSchema(rows.Count的实际值),进度条便能够正确确定终点。

Assigning a default value (100) to the TransferWorker's ReportProgress property caused the progress bar value to bloat past 100% when the value of MaxSchema was much smaller than that. MaxSchema的值远小于100%时,将默认值(100)分配给TransferWorker的ReportProgress属性会导致进度条值膨胀到100%以上。 (100*100)/4 = the reported value of 2500. (100 * 100)/ 4 =报告值2500。

Change the value of report progress to match the actual row count. 更改报告进度的值以匹配实际的行数。 In this case, the correct line would be: 在这种情况下,正确的行将是:

 TransferWorker.ReportProgress(MaxSchema);

Regarding the second issue of it working in the past with a significantly larger collection of rows, as stated by Peter: 关于过去的第二个问题,正如Peter所说,它具有更大的行集合:

 Dealing with more rows would prevent the problem, because you're dividing by MaxSchema. With enough rows, the result of the calculation is small, and with 2700000 rows, the result is 0, well below the default maximum value.`"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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