Progress bar problem
-
Hi, I am making an application that simply gets all the html from a webpage and filters out the data I want. The problem is that I want a progressbar to indicate it is working (using the "marquee" style). My code looks like this; Private void getWebData() { progressBar1.Style = ProgressBarStyle.Marquee; progressBar1.Value = 100; //simple code to get wepage... //simple code to filter out data I want... //put data into listbox progressBar1.Value = 0; progressBar1.Style = ProgressBarStyle.Continuous; } I thought that this would set the progressbar going, then do the work, then stop the progress bar, but instead the progress bar just doesnt do anything, although the code to get the html data does work. Any comment greatly appreciated, thanks! Martin.
-
Hi, I am making an application that simply gets all the html from a webpage and filters out the data I want. The problem is that I want a progressbar to indicate it is working (using the "marquee" style). My code looks like this; Private void getWebData() { progressBar1.Style = ProgressBarStyle.Marquee; progressBar1.Value = 100; //simple code to get wepage... //simple code to filter out data I want... //put data into listbox progressBar1.Value = 0; progressBar1.Style = ProgressBarStyle.Continuous; } I thought that this would set the progressbar going, then do the work, then stop the progress bar, but instead the progress bar just doesnt do anything, although the code to get the html data does work. Any comment greatly appreciated, thanks! Martin.
Martin23 wrote:
I thought that this would set the progressbar going, then do the work, then stop the progress bar, but instead the progress bar just doesnt do anything, although the code to get the html data does work.
If the entire form that the progress bar is on locks up, along with the progress bar not moving (i.e. nothing is redrawn on the form), then it is probably because you are running your webpage code from the same thread. The way to get around this is to run the web code on a seperate thread (although you shouldn't update your
ListBox
from this new thread, as that will throw errors). Hope that helps :) Cheers, Will H -
Martin23 wrote:
I thought that this would set the progressbar going, then do the work, then stop the progress bar, but instead the progress bar just doesnt do anything, although the code to get the html data does work.
If the entire form that the progress bar is on locks up, along with the progress bar not moving (i.e. nothing is redrawn on the form), then it is probably because you are running your webpage code from the same thread. The way to get around this is to run the web code on a seperate thread (although you shouldn't update your
ListBox
from this new thread, as that will throw errors). Hope that helps :) Cheers, Will HYes that does help, thnaks! Further problem now is how do I stop the progress bar? because if I put the code to stop the progress bar in the new thread, then that makes the progressbar disappears altogether when the thread finishes (why is that?), and I can't put the code like this; Private void getWebData() { progressBar1.Style = ProgressBarStyle.Marquee; progressBar1.Value = 100; //start new thread to do everything progressBar1.Value = 0; progressBar1.Style = ProgressBarStyle.Continuous; } because then as you would expect it starts and stop the progress bar imidiately. I assume I have to somehow attach an event handler to the new thread so I know when it finishes? thanks Martin
-
Yes that does help, thnaks! Further problem now is how do I stop the progress bar? because if I put the code to stop the progress bar in the new thread, then that makes the progressbar disappears altogether when the thread finishes (why is that?), and I can't put the code like this; Private void getWebData() { progressBar1.Style = ProgressBarStyle.Marquee; progressBar1.Value = 100; //start new thread to do everything progressBar1.Value = 0; progressBar1.Style = ProgressBarStyle.Continuous; } because then as you would expect it starts and stop the progress bar imidiately. I assume I have to somehow attach an event handler to the new thread so I know when it finishes? thanks Martin
-
Yes that does help, thnaks! Further problem now is how do I stop the progress bar? because if I put the code to stop the progress bar in the new thread, then that makes the progressbar disappears altogether when the thread finishes (why is that?), and I can't put the code like this; Private void getWebData() { progressBar1.Style = ProgressBarStyle.Marquee; progressBar1.Value = 100; //start new thread to do everything progressBar1.Value = 0; progressBar1.Style = ProgressBarStyle.Continuous; } because then as you would expect it starts and stop the progress bar imidiately. I assume I have to somehow attach an event handler to the new thread so I know when it finishes? thanks Martin
Maybe you've already worked it out, but I had a play and if you stick the following code somewhere, and then call
StopProgressBar()
from the new thread (after all the web stuff), it should work.private delegate void SetProgBarDelegate();
private void StopProgressBar()
{
if (this.InvokeRequired)
{
this.BeginInvoke(new SetProgBarDelegate(StopProgressBar));
return;
}
this.progressBar1.MarqueeAnimationSpeed = 0;
}Cheers, Will H
-
Maybe you've already worked it out, but I had a play and if you stick the following code somewhere, and then call
StopProgressBar()
from the new thread (after all the web stuff), it should work.private delegate void SetProgBarDelegate();
private void StopProgressBar()
{
if (this.InvokeRequired)
{
this.BeginInvoke(new SetProgBarDelegate(StopProgressBar));
return;
}
this.progressBar1.MarqueeAnimationSpeed = 0;
}Cheers, Will H
-
Wow, thanks Will, thats perfect! I'll remember to pass your kindness on to someone else! (plus I'll get a book on threading in .NET ;) ) Martin