Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C#

Never-ending Progress Bar (C#)

Rate me:
Please Sign up or sign in to vote.
4.33/5 (24 votes)
1 Oct 2004CPOL 136.4K   2.5K   55   19
The VB.NET Never-ending Progressbar now in C#

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)


Written By
United States United States
Visual Basic Developer since version 1.0
Java web developer
Currently developing in vb and c#

Comments and Discussions

 
GeneralAwesome Pin
Jboy20130-Mar-07 7:12
Jboy20130-Mar-07 7:12 
GeneralHmmmm Pin
tayspen20-Nov-05 17:11
tayspen20-Nov-05 17:11 
GeneralRe: Hmmmm Pin
tayspen30-Nov-05 9:18
tayspen30-Nov-05 9:18 
GeneralGreat control, but few idea's & Issue's Pin
Priest Of Psi4-Oct-04 22:54
Priest Of Psi4-Oct-04 22:54 
GeneralRe: Great control, but few idea's & Issue's Pin
GregOsborne5-Oct-04 4:39
GregOsborne5-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's Pin
Haiyan Du2-May-08 5:00
Haiyan Du2-May-08 5:00 
GeneralRe: Can you post your sample working code!Re: Great control, but few idea's &amp; Issue's Pin
Haiyan Du2-May-08 6:22
Haiyan Du2-May-08 6:22 
GeneralRe: Great control, but few idea's &amp; Issue's Pin
GregOsborne5-Oct-04 4:49
GregOsborne5-Oct-04 4:49 
GeneralFlickering... and solution Pin
The Bug2-Oct-04 12:45
The Bug2-Oct-04 12:45 
GeneralRe: Flickering... and solution Pin
GregOsborne4-Oct-04 4:31
GregOsborne4-Oct-04 4:31 
GeneralRe: Flickering... and solution Pin
polyester16-Nov-04 10:49
polyester16-Nov-04 10:49 
GeneralRe: Flickering... and solution Pin
GregOsborne16-Nov-04 10:55
GregOsborne16-Nov-04 10:55 
GeneralRe: Flickering... and solution Pin
polyester17-Nov-04 7:07
polyester17-Nov-04 7:07 
GeneralGreat! but... Pin
Curtis W2-Oct-04 11:24
Curtis W2-Oct-04 11:24 
GeneralRe: Great! but... Pin
GregOsborne4-Oct-04 4:29
GregOsborne4-Oct-04 4:29 
GeneralRe: Great! but... Pin
Curtis W4-Oct-04 16:00
Curtis W4-Oct-04 16:00 
GeneralRe: Great! but... Pin
Anonymous4-Oct-04 16:55
Anonymous4-Oct-04 16:55 
GeneralRefreshing Problem + Workaround Pin
Orcrist1-Oct-04 11:25
Orcrist1-Oct-04 11:25 
GeneralRe: Refreshing Problem + Workaround Pin
GregOsborne1-Oct-04 11:28
GregOsborne1-Oct-04 11:28 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.