Hi GaniTPT,
Based on your code, there is a problem that the value of progbar1 will exceed his maximum value in the foreach loop.
And to avoid blocking the UI thread, I suggest you use BackgroundWorker to update the progress.
Here is a code example you can refer to.
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Maximum = dt.Rows.Count;
progressBar1.Value = 0;
progressBar1.Step = 3;
progressBar1.Minimum = 0;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();
}
DataTable dt = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i <= 1000; i++)
{
DataRow row = dt.NewRow();
dt.Rows.Add(row);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
var backgroundWorker = sender as BackgroundWorker;
for (int j = 0; j <= dt.Rows.Count; j++)
{
backgroundWorker.ReportProgress((j * progressBar1.Maximum) / dt.Rows.Count);
}
}
Best Regards,
Daniel Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.