gpt4 book ai didi

c# - 计算的进度百分比未更新

转载 作者:太空宇宙 更新时间:2023-11-03 21:06:34 25 4
gpt4 key购买 nike

我正在尝试通过标签中的后台工作人员显示进展的百分比值。我正在添加 Aprox。 25,000 行数据表。当我将 Label.Text 设置为 e.ProgressPercentage only 时它工作正常。但是当我计算 % 值时,它保持不变。只有在 worker 完成后标签更新为 100%

                progressCount = report.Rows.Count;
foreach (DataRow r in report.Rows)
{

rp.pName = r[1].ToString();
rp.batch = r[2].ToString();
rp.expr = r[3].ToString();
rp.stock = r[5].ToString();
rp.rate = r[6].ToString();
backgroundWorker2.ReportProgress(i, rp);
System.Threading.Thread.Sleep(2);
if(backgroundWorker2.CancellationPending)
{
e.Cancel = true;
backgroundWorker2.ReportProgress(0);
}
i++;

}



private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
metroProgressBar1.Minimum = 0;
metroProgressBar1.Maximum = progressCount;
stock st = new stock();
reportClass rp = (reportClass)e.UserState;
if(!backgroundWorker2.CancellationPending)
{
st.stockReport.Rows.Add(rp.pName, rp.batch, rp.expr, rp.stock, rp.rate);
metroProgressBar1.Value = e.ProgressPercentage;
int percn = (e.ProgressPercentage / progressCount) * 100;
metroLabel4.Text =percn.ToString();
}
}

最佳答案

您很可能将整数除以整数,结果为零。先将您的值转换为 double,进行计算,然后将结果转换回 int。

改变:

int percn = (e.ProgressPercentage / progressCount) * 100;

收件人:

int percn = (int)(((double)e.ProgressPercentage / (double)progressCount) * 100);

关于c# - 计算的进度百分比未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40829823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com