How to attach a process bar with a process in C#...
Let we want to add 1 to 100 numbers in a list box and runs a progress bar to show the process percentage.
1. Create a window form application. Design a form with a Listbox to insert the values and with a progress bar to show the process. Also a button to start that process.
2. Now add a backgroundworker control from toolbox. That will add at the bottom of form.
3. You have to add a method for that task you want to perfrom (i.e. in this example we want to add 1 to 100 number in a list. so i create a task method) with the parameters BackgroundWorker bw and DoWorkEventArgs  like 
4. Set Property of Backgroundworker controls.
WorkerReportProcess=true
WorkerSupportCance=true
from property window
   public void task(BackgroundWorker bw, DoWorkEventArgs e)
        {
            if (bw.CancellationPending)  // if cancel button pressed  
            {
                e.Cancel = true;  
            }
            else
            {
                for (int i = 0; i<= 100; i++)
                {
                    listBox1.Items.Add(i);  
                    bw.ReportProgress(i);  //Gives the report of process (value of i as process %age used).
                    System.Threading.Thread.Sleep(1000); // for delay in execution
                }
            }
        } 
     5. Generate the BacgroundWorker.Dowork event by clicking the event from events of backgroundworker.
         
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bw = (BackgroundWorker)sender;
task(bw, e);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bw = (BackgroundWorker)sender;
task(bw, e);
}
  6. To Change the process percentage of ProgressBar1  use ProgressChanged event of BackgroundWorker       control.
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
7. To show the massage when the process is completed. work on RunWorkerCompleted event of backgroundWorker1 like
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Work Compleated!!!");
}
8. We use "Thread.Sleep(...)" method of threading. so we also have to set CheckForIllegalCrossThreadCalls = false; on page_load event
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
9. To start procss on the button write in button1_click(...)
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
10. You may take a "Cancel" button on form to cancel the running process. For cancel the process you write the code.
private void button2_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
Now as you click the Start Process button the process will start and progress bar show how much process is completed!!!private void button2_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
Write your comment about this post and also welcome for the batterment suggestions...