C# C WinForm BackgroundWorker未更新Processbar
我在让backgroundworker更新我的进度条方面遇到了一些问题。我使用在线教程作为示例,但我的代码不起作用。我在这个网站上做了一些挖掘,但我找不到任何解决方案。我不熟悉背景工作/进展方面的事情。所以我不完全理解 只是想把事情安排好: 我有一个主窗体Form1,它打开另一个带有进度条和状态标签的窗体3 我的表格3代码如下:C# C WinForm BackgroundWorker未更新Processbar,c#,winforms,backgroundworker,C#,Winforms,Backgroundworker,我在让backgroundworker更新我的进度条方面遇到了一些问题。我使用在线教程作为示例,但我的代码不起作用。我在这个网站上做了一些挖掘,但我找不到任何解决方案。我不熟悉背景工作/进展方面的事情。所以我不完全理解 只是想把事情安排好: 我有一个主窗体Form1,它打开另一个带有进度条和状态标签的窗体3 我的表格3代码如下: public string Message { set { lblMessage.Text = value; } } public int ProgressV
public string Message
{
set { lblMessage.Text = value; }
}
public int ProgressValue
{
set { progressBar1.Value = value; }
}
public Form3()
{
InitializeComponent();
}
我的表格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();
}
所以。问题是它没有更新任何东西。进度条或标签正在更新。有人能告诉我写的方向或者有什么建议吗?这会给你0*10,因为count和_totalRecords是整数值,这里使用整数除法。因此,计数小于总记录数,则公式等于0:
当所有的工作都完成后,公式等于1。但这就是进步没有改变的原因
以下是正确的百分比计算:
int percent = count * 100 / _totalRecords;
这将得到0*10,因为count和_totalRecords是整数值,这里使用整数除法。因此,计数小于总记录数,则公式等于0:
当所有的工作都完成后,公式等于1。但这就是进步没有改变的原因
以下是正确的百分比计算:
int percent = count * 100 / _totalRecords;
您只能在工作完成之前报告进度
worker.ReportProgress(percent, string.Format("Completed record {0} out of " + _totalRecords, count));
// You exit DoWork right after reporting progress
尝试在BackgroundWorker运行时定期报告进度。还要检查Jon的评论,以确保WorkerReportsProgress设置为true。您只能在工作完成之前报告进度
worker.ReportProgress(percent, string.Format("Completed record {0} out of " + _totalRecords, count));
// You exit DoWork right after reporting progress
尝试在BackgroundWorker运行时定期报告进度。还要检查Jon的注释,以确保WorkerReportsProgress设置为true。您需要将整数值转换为双精度,否则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));
}
}
您需要将整数值强制转换为双精度,否则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));
}
}
所以我做了更多的挖掘
未设置告诉对象哪些函数要转到哪些对象的属性:/
谢谢你的帮助,所以我做了更多的挖掘工作
未设置告诉对象哪些函数要转到哪些对象的属性:/
感谢您的帮助您是否设置了BackgroundWorker.WorkerReportsProgress?DoWork中的代码处于while循环中,对吗?不要简化太多。你设置BackgroundWorker.WorkerReportsProgress了吗?DoWork中的代码在一个while循环中,对吗?不要过于简化。