tags:

views:

80

answers:

5

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.

+3  A: 

My guess is that you're doing all the work in the UI thread. Don't do that.

Use BackgroundWorker to do the work, periodically updating the progress bar appropriately, using ReportProgress to allow the UI change to be made in the UI thread.

If you search for BackgroundWorker tutorials you'll find a whole bunch of them, such as this one. Many will give examples using progress bars, as BackgroundWorker is pretty much designed for that scenario.

Jon Skeet
http://www.devtoolshed.com/content/c-download-file-progress-bar Link to a site for an example. ;)
Emerion
@Emerion: I already linked to a site with an example...
Jon Skeet
@ Jon woops!, problably read over the link, sorry! ;)
Emerion
A: 

By default if you are running a long operation the winform is not going to repaint itself. To fix it consider:

  1. Running your computations on a separate thread, preferably using BackgroudWorker
  2. Repainting it manually every time you change your progress bar.

(1) is strongly recommended.

Grzenio
+1  A: 

For that to work, you need to let the form paint when necessary. The correct / preferred way is to use a worker thread (perhaps BackgroundWorker) for the long running tasks, calling back to the UI thread periodically to ask it to update the progress.

There is also DoEvents, but that is (frankly) a hack.

Marc Gravell
+1  A: 

The easiest but not correct way to fix the issue is to use the following inside your loop:

Application.DoEvents();

The more challenging is to use separate thread for calculations and current UI thread for updating the progress bar.

Bashir Magomedov
Might sometimes work, but very bad practice
smirkingman
I said that, if you haven't noticed. And it is not only my advice. Look around, man. Why are you lowering mine answer?
Bashir Magomedov
The difference between 'not correct' and 'very bad practice'. DoEvents is not a solution at all, even suggesting it is awfully bad advice, likely to lead the OP down the wrong path.
smirkingman
Yes, I agree. But I'm not saying rubbish here, like “This is the best choice ever”. So I think that downvoting is not an option in this case, moreover some other guys replied with the same suggestion, and all of us, in one way or other, stated here that it is not a good solution.Let’s say that now everything is ok. Hope you are right and wasn’t offended. Also hope that the guy who asked the question will not ever use DoEvents() in his life :)
Bashir Magomedov
A: 

As suggested, use a BackGroundWorker.

In the worker, DO NOT update the ProgressBar directly, do it with BeginInvoke.

smirkingman
Except for the typo (Back **g** roundWorker) - why was this downvoted?
nikie
cause he downvoted mine without any reason
Bashir Magomedov
Please grow up (and give me more than 30 seconds to reply to your complaint)
smirkingman