C# 中的BackgroundWorker 问题

发布于 2024-11-09 13:24:53 字数 1703 浏览 0 评论 0原文

我的组件“BackgroundWorker”有问题

当我单击按钮时,我必须执行 n 次迭代,这需要时间,并且我必须将此操作委托给

我遵循本教程的另一个线程: 教程(在french)

这是我的代码:

    private void btnIterate_Click(object sender, EventArgs e)
    {
        bgwIterer.RunWorkerAsync();                
    }

    private void bgwIterer_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        e.Result = new Iterate(btnIterate, btnReinit, txtInput, lblState, entree, worker, e);
    }

    private void bgwIterer_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        this.lblState.Text = e.ProgressPercentage;
    }

bgwIterer 是我的 BackgroundWorker 组件 这是我想要委托的方法:

类迭代
{
    // 迭代类的附加构造函数:
    公共迭代(按钮 mybtnIterate,按钮 mybtnReinit,
    文本框 mytxtInput,标签 mylblState,
    int myentree,BackgroundWorker工人,
    DoWorkEventArgs e)
    {
        int 倾角 = 0;
        int var0 = 0, var1;
        mybtnIterate.Enabled = false;
        mytxtInput.Focus();
        做
        {
            变量1=0;
            做
            {
                ++var1;
            }
            while (var1 < myentree);
            ++var0;
            浇注=(var0 / myentree)* 100;
            工人.ReportProgress(pourcent);
        }
        while (var0 < myentre);
        mylblState.Text = "终止!";
        mytxtInput.Enabled = false;
    }

}

就是问题所在:

找不到类型或命名空间名称“BackGroundWorker”(您是否缺少 using 指令或程序集引用?)

有人有想法吗?

I have a problem with a component "BackgroundWorker"

When I click on a button I have to perform n iterations that take time and I have to delegate this operation to another thread

I followed this tutorial: tutorial (in french)

Here is my code:

    private void btnIterate_Click(object sender, EventArgs e)
    {
        bgwIterer.RunWorkerAsync();                
    }

    private void bgwIterer_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        e.Result = new Iterate(btnIterate, btnReinit, txtInput, lblState, entree, worker, e);
    }

    private void bgwIterer_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        this.lblState.Text = e.ProgressPercentage;
    }

bgwIterer is my BackgroundWorker component
and here is the method I would like to delegate:

class Iterate
{
    // Constructeur surchargé de la classe Iterate:
    public Iterate(Button mybtnIterate, Button mybtnReinit,
    TextBox mytxtInput, Label mylblState,
    int myentree, BackgroundWorker worker,
    DoWorkEventArgs e)
    {
        int pourcent = 0;
        int var0 = 0, var1;
        mybtnIterate.Enabled = false;
        mytxtInput.Focus();
        do
        {
            var1 = 0;
            do
            {
                ++var1;
            }
            while (var1 < myentree);
            ++var0;
            pourcent = (var0 / myentree) * 100;
            worker.ReportProgress(pourcent);
        }
        while (var0 < myentree);
        mylblState.Text = "Terminé !";
        mytxtInput.Enabled = false;
    }

}

and this is the problem:

The type or namespace name 'BackGroundWorker' could not be found (are you missing a using directive or an assembly reference?)

Does someone have an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似梦非梦 2024-11-16 13:24:53

尝试添加:

using System.ComponentModel;

Try to add:

using System.ComponentModel;
很酷不放纵 2024-11-16 13:24:53

类名是BackgroundWorker,而不是BackGroundWorker - 这就是原因。

Class name is BackgroundWorker, not BackGroundWorker - that's why.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文