Struggling to get ProgressBar updated (without threading)

1.1k views Asked by At

i have a program(winform) that runs through a lot of files(in the area of 400 some times) in a folder and to show its progressing i use a progress bar. the info get posted into a listview if that makes a difference. The problem i am having is that when the pc is slow, im working with folders over a network or if it is a really big amount of files the ProgressBar just stops updating. if n leave the program it will continue and finish, but the progress bar gets stuck at some stage. And then only shows that it is complete (additional note: i have noticed that isnt just the progress bar. sometimes it is the whole form. but again, when the program is done it is fine. and while it is working you can move the form, just not interact with it)

Now here is where it gets tricky. I know it can be solved by using threads. i have however been told not to use it(let us not go into the why, lets just except it, whether it is stupid or not). i have also tried to refresh the form, and to refresh the bar itself. none of it seems to work. it isn't a good idea to use it, but even tried .DoEvent, it also doesn't work

How can i get the progress bar to update? any ideas?

here is some of the code i use for the progress bar

//before the loop
progress = iCount1;
progressvalue = 0;
double increment = 100 / (double)progress;
//this is at the end of the loop
progressvalue = progressvalue + increment;

then there is a bit of code. some of it to stop the value from going out of bound. the rest not related to the progress bar at all

edit: o, i have that 1 variable. and it basically runs over 3 loops. so that we have one continuous bar. not the bar running 3 times

edit: it seems to work fine for folder on my pc. but when it is run on a slow pc or over a network, that is when it does this

EDIT:Im getting answers about backgroundworker and threads. so is there no way to do it without threads?

Update: i finally convinced my boss to do it with backgroundworker. so ill be doing it in that. but for this question i guess the .DoEvent answer is the best

2

There are 2 answers

0
Alex On BEST ANSWER

You could add an Application.DoEvents() then.

For example:

while(!end)
{ 

            //You processing logic here
            Application.DoEvents();
}

Note that it is generally discouraged since it will break the normal flow of events and may have unpredicted results.

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents.aspx http://www.codinghorror.com/blog/2005/08/is-doevents-evil-revisited.html

1
Henrik Gering On

When the form-thread is busy, it will be very unresponsive and not draw the form at all. Even if you dont want threading i think you ought to reconsider..

I would use a backgroundworker as in this example: here it is a quite simple setup.