C# WinForm BackgroundWorker not Updating Processbar

别说谁变了你拦得住时间么 提交于 2019-12-12 17:35:16

问题


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.

my Form 3 code as is:

public string Message
{
    set { lblMessage.Text = value; }
}

public int ProgressValue
{
    set { progressBar1.Value = value; }
}
public Form3()
{
    InitializeComponent();
}

My Form 1 Partial Code:

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?


回答1:


That will give you 0 * 10 because count and _totalRecords are integer values, and integer division is used here. Thus count is less than total records, you have formula equal to 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. But that's the reason why progress is not changing.

Here is correct percentage calculation:

int percent = count * 100 / _totalRecords;



回答2:


You need to cast your INTEGER value to a DOUBLE, or C# Math will/could truncate it to 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));
  }
}



回答3:


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. Also check Jon's comment to ensure WorkerReportsProgress is set to true.




回答4:


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



来源:https://stackoverflow.com/questions/17981019/c-sharp-winform-backgroundworker-not-updating-processbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!