A background worker is very, very easy to do:
Create a BackgroundWorker instance, and hook it to a couple of events:
private BackgroundWorker worker = new BackgroundWorker();
...
butDoMyTask.Enabled = false;
ShowProgress.Visible = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.WorkerReportsProgress = true;
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerAsync();
Then in the events, you just have to show progress, do the actual work, and mark when it completes:
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
butDoMyTask.Enabled = true;
ShowProgress.Visible = false;
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ShowProgress.Value = e.ProgressPercentage;
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 100; i += 10)
{
Thread.Sleep(1000);
worker.ReportProgress(i);
}
}