Click here to Skip to main content
Click here to Skip to main content

Never-ending Progress Bar (C#)

By , 1 Oct 2004
 

Sample Image - OSProgress.png

Introduction

Sometimes you just don’t know how long something will take. Like when your wife says she’ll only be a few minutes in the grocery store…you know what I mean.

The standard ProgressBar that ships with Visual Studio is great for when you know how long something will take, or there is a way to determine just how many steps a process will have. But like the trip to the store with your wife, sometimes you just can’t know how long something’s going to take. That’s why I created this progress bar.

More Information

For more information, please see http://www.codeproject.com/vb/net/NeverEndingBar.asp for the original article and VB.NET source.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Greg Osborne
United States United States
Member
Visual Basic Developer since version 1.0
Java web developer
Currently developing in vb and c#

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralAwesomememberJboy20130 Mar '07 - 7:12 
This program is absoulutely awesome, however the in-code documentation is lacking. Good program though.Laugh | :laugh:
GeneralHmmmmmembertayspen20 Nov '05 - 17:11 
Now i must make it not into a DLL
Smile | :)
GeneralRe: Hmmmmmembertayspen30 Nov '05 - 9:18 
Seriously though what is the lisense on this?
GeneralGreat control, but few idea's & Issue'smemberPriest Of Psi4 Oct '04 - 22:54 
Hey, great control & idea. A few issue's though, perhaps when the visibility of the control is false, and the timer is active, surely you wouldnt want the ticker too continue repainting.
 
Also, if the current thread that it is executed on is busy, the control doesnt visually update, any idea's or suggestions on how too work around this?
 
Drugs are bad, very very bad. Now give them to me for safe keeping Smile | :)
GeneralRe: Great control, but few idea's & Issue'smemberGregOsborne5 Oct '04 - 4:39 
If you look at the original post in the VB.NET section it explains that if you have a process that blocks, or is busy, whatever is running in that thread will block, hence the control stops scrolling. The class below can be used as a class to <[insert here your process]> on another thread. I'm using it for xml serialization. I'm assuming you are using the progressbar as some sort of indicator on a form while you are processing. Start your progressbar, create an instance of this class, modifying it so that it performs your process, and then call the spinup method When it is complete, the LoadComplete property will be set to true. That's how you will know it's done. Make sure you call the SpinDown method when done. That releases the thread. THat's all there is to it...threading's so COOL!Suspicious | :suss:
 
Imports System.Threading
Imports System.io
Imports System.xml.Serialization
Friend Class DocLoaderThread
Private _Thread As Thread
Private _Document As Document
Private _FileName As String
Private _IsCancelled As Boolean
Private _LoadComplete As Boolean
Public ReadOnly Property LoadedDocument() As Document
Get
Return _Document
End Get
End Property
Public ReadOnly Property LoadComplete() As Boolean
Get
Return _LoadComplete
End Get
End Property
Private Sub Start()
Try
Dim xmlser As New XmlSerializer(GetType(Document))
Dim reader As New StreamReader(_FileName)
_Document = xmlser.Deserialize(reader)
reader.Close()
Catch ex As Exception
_Document.SetLoadError(ex)
Finally
Me._LoadComplete = True
End Try
End Sub
Private Sub Cancel()
_IsCancelled = True
End Sub
Public Sub Spinup(ByVal FileName As String)
_Document = New Document
_FileName = FileName
Dim ThreadStart As New ThreadStart(AddressOf Me.Start)
_Thread = New Thread(ThreadStart)
_Thread.Start()
End Sub
Public Sub SpinDown()
Cancel()
_Thread.Join()
_Thread = Nothing
End Sub
End Class

 
Greg Osborne
Microsoft Certified Professional Since 1995
GeneralCan you post your sample working code!Re: Great control, but few idea's &amp; Issue'smemberHaiyan Du2 May '08 - 5:00 
Can you post your sample code? I use this during object Deserialization which is similar to your task. The progress bar becomes idole during the object deserialization.
 
Haiyan DU

GeneralRe: Can you post your sample working code!Re: Great control, but few idea's &amp; Issue'smemberHaiyan Du2 May '08 - 6:22 
I finished this task with the System.ComponentModel.BackgroundWorker
You have progressbar which autoprogress set to true and visible set to false.
when user start the program, no one will see this progress bar. when user click a button, this progress bar visible is set to true and we call SpineUp() now.
Big Grin | :-D Big Grin | :-D
here is my code:
---------------------
private System.ComponentModel.BackgroundWorker _backThread;
public Form1()
{
InitializeComponent();
_backThread=new System.ComponentModel.BackgroundWorker();
this._backThread.DoWork += new System.ComponentModel.DoWorkEventHandler(this.ProcessNumbersBackgroundWorker_DoWork);
this._backThread.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.ProcessNumbersBackgroundWorker_RunWorkerCompleted);


}

private void ProcessNumbersBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
//Do your work here
}
 
private void ProcessNumbersBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this._ProgressBarPanel.Visible=false;
MessageBox.Show("Done background work!");

}
private void button4_Click(object sender, EventArgs e)
{

this._ProgressBarPanel.Visible=true;
SpineUp();

}
private void SpineUp()
{

_backThread.RunWorkerAsync();

}
 
Haiyan DU

GeneralRe: Great control, but few idea's &amp; Issue'smemberGregOsborne5 Oct '04 - 4:49 
Another note...don't be surprised if it still blocks when you are trying to debug it. I have experienced this in the VS-IDE with my current installation, but not with others in the past. It may be a configuration problem on my part. It soes work fine though compiled running outside the ide
 
Greg Osborne
Microsoft Certified Professional Since 1995
GeneralFlickering... and solutionmemberLEXXa2 Oct '04 - 12:45 
Hi,
This one from that thingy controls everyone can code but lazy to. Smile | :) )
 
Thanks, as well! Great work.
 
As always, some notes about flickering: paste this inside InitializeComponent()
 
this.ResizeRedraw = true;
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
 
Looks better after that.
 

GeneralRe: Flickering... and solutionmemberGregOsborne4 Oct '04 - 4:31 
Outstanding!
 
Thanks for the tip
 
Greg Osborne
Microsoft Certified Professional Since 1995
GeneralRe: Flickering... and solutionmemberpolyester16 Nov '04 - 10:49 
I've implemented this code in VB.NET and it works great, but I don't understand why I had to comment out SetStyle(ControlStyles.UserPaint, True), which causes the control to become invisible..? Could it be because I have it subclassed inside a statusbar? Why does it work in C# for you guys?!
GeneralRe: Flickering... and solutionmemberGregOsborne16 Nov '04 - 10:55 
This has already been implemented in VB Go to here for the code
 
http://www.codeproject.com/vb/net/NeverEndingBar.asp
 
Don't know why that makes the bar invisible...I never had that behavior when developing it
 
Greg Osborne
Microsoft Certified Professional Since 1995
GeneralRe: Flickering... and solutionmemberpolyester17 Nov '04 - 7:07 
Yeah, I'm using the VB version, but posted the question here because I ported LEXXa's C# flickering solution code to VB in order to resolve some of the flickering issues. I'm just trying to understand why the SetStyle(ControlStyles.UserPaint, True) call didn't fly, but the rest of them did (and reduced the flickering). While the flickering situation has been improved greatly as a result of using LEXXA's code in ProgressStatus.New(), there still is a little bit that is noticeable, and I'm wondering if it can be eliminated completely if I can somehow set UserPaint without the control disappearing. Since I've got my instance of the control subclassed within a StatusBar, perhaps I have to make these calls on the parent StatusBar instead/also?
GeneralGreat! but...memberCurtis W2 Oct '04 - 11:24 
You should include a screenshot of what it ACTUALLY looks like. I was dissapointed after I got it running that it looks nothing like the screenshot...

GeneralRe: Great! but...memberGregOsborne4 Oct '04 - 4:29 
It is what it actually looks like. The graphics on the screen shot were from my own implementation of the bar on a status form. I added the graphics to the ends and text boxes and used graphics for the progress. Flash it up any way you want
 
Greg Osborne
Microsoft Certified Professional Since 1995
GeneralRe: Great! but...memberCurtis W4 Oct '04 - 16:00 
Cool. Care to include that with the sample?
 
Thanks,
Curtis.
GeneralRe: Great! but...sussAnonymous4 Oct '04 - 16:55 
No...but thanks for askingBig Grin | :-D
GeneralRefreshing Problem + WorkaroundmemberOrcrist1 Oct '04 - 11:25 
Thanks, Nice control.
 
This is certainly outside of normal (probable) usage but I thought I'd pass on a workaround to a problem I ran into when using the osProgress within a container That is itself resized.
 
To Reproduce:
1.Add a Panel with Dock=Top
2.Add the osProgressBar1 to the Panel
3.Start the application
4.Resize the form back and forth from multiple times.. It's inconsistent but eventually the progress Bar will no longer display the highlighted block (although it is probably working in the background).
 
To workaround the problem I just added:
osProgressBar1.Position = 0 to the Resize event of the Form
 
Cheers,

GeneralRe: Refreshing Problem + WorkaroundmemberGregOsborne1 Oct '04 - 11:28 
Thanks...I actually found that one a few minutes ago myself
 
Greg Osborne
Microsoft Certified Professional Since 1995

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 1 Oct 2004
Article Copyright 2004 by Greg Osborne
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid