Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# problem with BackgroundWorker

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?

like image 987
Michaël Avatar asked May 24 '11 12:05

Michaël


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

Try to add:

using System.ComponentModel;
like image 129
Stecya Avatar answered Sep 20 '22 02:09

Stecya


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

like image 20
Andrey Avatar answered Sep 23 '22 02:09

Andrey