Add a Background Worker and your code should (or could) end up looking something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
backgroundWorker1.ReportProgress(Convert.ToInt32((countsofar / totalofthingstodo) * 100));
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar1.Value = progressBar1.Minimum;
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
}
For the above to work the BackgroundWorker must have WorkerReportsProgress set to True.
If you can't calculate the percentage progress then make the progressBar a Marquee (Style = Marquee) and don't report progress, just have it default to disabled (Enabled = False), enable it at the start of the job to be done and disable it in the
backgroundWorker1_RunWorkerCompleted
method.
Mike