C#WinForm BackgroundWorker不更新进程栏 [英] C# WinForm BackgroundWorker not Updating Processbar

查看:264
本文介绍了C#WinForm BackgroundWorker不更新进程栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让背景工作人员更新进度条时遇到了一些麻烦.我以在线教程为例,但是我的代码无法正常工作.我在此站点上进行了一些挖掘,但找不到任何解决方案.我是背景工作人员/进度方面的新手.所以我不太了解.

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.

只需进行设置: 我有一个主窗体(FORM 1),它打开了另一个带有进度条和状态标签的(FORM 3).

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 3代码如下:

my Form 3 code as is:

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();
}

所以.问题在于它没有更新任何内容.进度栏或标签正在更新.有人可以指出我的写作方向或提出建议吗?

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?

推荐答案

这将为您提供0 * 10,因为count_totalRecords是整数值,此处使用整数除法.因此,count小于总记录,则formula等于0:

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

好吧,当所有工作完成后,您将得到formula等于1的信息.但这就是进步没有改变的原因.

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;

这篇关于C#WinForm BackgroundWorker不更新进程栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆