[英]C# WinForm BackgroundWorker not Updating Processbar
I am having a little trouble with getting the backgroundworker to update my progress bar. 让背景工作人员更新进度条时遇到了一些麻烦。 I was using a tutorial online as an example, but my code is not working.
我以在线教程为例,但是我的代码无法正常工作。 I did some digging on this site and I can't find any kind of solution.
我在此站点上进行了一些挖掘,但找不到任何解决方案。 I am new to the backgroundworker/progress thing.
我是背景工作人员/进度方面的新手。 So I don't understand it fully.
所以我不太了解。
Just to set things up: I have a main form (FORM 1) that opens another (FORM 3) with the progress bar and a status label. 只是为了进行设置:我有一个主窗体(FORM 1),它打开了另一个带有进度条和状态标签的窗体(FORM 3)。
my Form 3 code as is: 我的Form 3代码如下:
public string Message
{
set { lblMessage.Text = value; }
}
public int ProgressValue
{
set { progressBar1.Value = value; }
}
public Form3()
{
InitializeComponent();
}
My Form 1 Partial Code: 我的表格1部分代码:
private void btnImport_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy != true)
{
if (MessageBox.Show("Are you sure you want to import " + cbTableNames.SelectedValue.ToString().TrimEnd('$') + " into " + _db, "Confirm to Import", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
alert = new Form3(); //Created at beginning
alert.Show();
backgroundWorker1.RunWorkerAsync();
}
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
int count = 0
foreach(DataRow row in DatatableData.Rows)
{
/*... Do Stuff ... */
count++;
double formula = count / _totalRecords;
int percent = Convert.ToInt32(Math.Floor(formula)) * 10;
worker.ReportProgress(percent, string.Format("Completed record {0} out of " + _totalRecords, count));
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
alert.Message = (String) e.UserState;
alert.ProgressValue = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
alert.Close();
}
So. 所以。 The problem is its not updating anything.
问题是它没有更新任何内容。 The progress bar nor the label is updating.
进度栏或标签正在更新。 Can someone point me in the write direction or have a suggestion?
有人可以指出我的写作方向或提出建议吗?
That will give you 0 * 10
because count
and _totalRecords
are integer values, and integer division is used here. 这将为您提供
0 * 10
因为count
和_totalRecords
是整数值,此处使用整数除法。 Thus count
is less than total records, you have formula
equal to 0
: 因此,
count
小于总记录,则您的formula
等于0
:
double formula = count / _totalRecords; // equal to 0
int percent = Convert.ToInt32(Math.Floor(formula)) * 10; // equal to 0
Well, when all work is completed, you will have formula
equal to 1
. 好吧,当所有工作完成时,您将拥有等于
1
formula
。 But that's the reason why progress is not changing. 但这就是进步没有改变的原因。
Here is correct percentage calculation: 这是正确的百分比计算:
int percent = count * 100 / _totalRecords;
You need to cast your INTEGER value to a DOUBLE, or C# Math will/could truncate it to 0: 您需要将INTEGER值强制转换为DOUBLE,否则C#Math将/可能将其截断为0:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
var worker = (BackgroundWorker)sender;
for (int count = 0; count < _totalRecords; count++) {
/*... Do Stuff ... */
double formula = 100 * ((double)count / _totalRecords); // << NOTICE THIS CAST!
int percent = Convert.ToInt32(formula);
worker.ReportProgress(percent, string.Format("Completed record {0} out of " + _totalRecords, count));
}
}
You only report progress JUST before the work completes 您仅在工作完成之前报告进度
worker.ReportProgress(percent, string.Format("Completed record {0} out of " + _totalRecords, count));
// You exit DoWork right after reporting progress
Try reporting progress periodically while the BackgroundWorker is running. 尝试在BackgroundWorker运行时定期报告进度。 Also check Jon's comment to ensure WorkerReportsProgress is set to true.
还要检查Jon的注释,以确保WorkerReportsProgress设置为true。
So i did more digging the properties that tells the object which tell the object which functions to go to weren't set :/ 所以我做了更多的工作来挖掘告诉对象的属性,该属性告诉对象未设置要去的功能:/
thanks for your help 谢谢你的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.