I am creating in my main form a BackgroundWorker object and I have two click events, one to activate and the other to disable backgroundWorker, but the method of disable is not working.
BackgroundWorker worker;
public FrmPrincipal()
{
InitializeComponent();
worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
}
Activate button:
private Ativar_Click()
{
worker.RunWorkerAsync();
}
Deactivate button:
private Desativar_Click()
{
worker.CancelAsync();
}
Do_Work Event:
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
while(true)
{
ClienteBusiness.Inserir();
}
}