What happens is when I click the button to run my code my win form locks up and my progress bar doesn’t run. What am I doing wrong?
foreach (string barcount in idit)
{
barcountmax++;
}
label2.Text = "Total entries: " + barcountmax;
progressBar1.Minimum = 0;
progressBar1.Maximum = barcountmax;
...
foreach (string ids in idit)
{
string[] splitids = ids.Split('|');
string fixcommacrap = "";
barmovement++;
progressBar1.Value = barmovement;
}
My Winform just locks and freezes, i’m unable to see the progress bar.
My guess is that you’re doing all the work in the UI thread. Don’t do that.
Use
BackgroundWorkerto do the work, periodically updating the progress bar appropriately, usingReportProgressto allow the UI change to be made in the UI thread.If you search for
BackgroundWorkertutorials you’ll find a whole bunch of them, such as this one. Many will give examples using progress bars, asBackgroundWorkeris pretty much designed for that scenario.