C# problem with BackgroundWorker
I have a problem with a component "BackgroundWorker"
When I click on a button I have to perform n iteration开发者_StackOverflows 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?
Try to add:
using System.ComponentModel;
Class name is BackgroundWorker, not BackGroundWorker - that's why.
精彩评论