C# backgroundworker

BackgroundWorker provides an interface to show the work at background.

System.ComponentModel;
BackgroundWorker bgw = new BackgroundWorker();

Cancellation and progress report are two important properties.
bgw.WorderSupportsCancellation; //whether supports cancellation or not
bgw.WorkerReportsProgress; //whether reports progress or not
bgw.ReportProgress(20); //report pregress 20%
bgw.CancelAsync; //calcel it

An example of BackgroundWorder handling button click in Visual C#.
using System.Threading.Thread;
void dotButton_Click(object sender, EventArgs e)
{
BackgroundWorker bgw = sender as BackgroundWorker;
for (int i = 1; i < 50; i++)
{
if (bgw.CancellationPending == true)
{
break;
}
Sleep(1000);
int k = i * 2;
bgw.ReportProgress(k);
}
}


endmemo.com © 2024  | Terms of Use | Privacy | Home