Click here to Skip to main content
12,953,937 members (42,443 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 14 Jul 2014

VB.Net Progress Bar - Single or Multi-Threaded

, 15 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
A dynamic progress bar control easy to use and literally plug and play


My first article ever, in any community. A progress bar is something that definitely any developer will need at some point. I've read some other articles about the same topic here but found them unnecessarily complicated, both for beginners and also for the more experient. If you are a beginner, you don't need to understand the code right away. You only need to know how to instantiate a class and update its properties as your task progresses. If you are experient you'll understand the code (and maybe enhance it) but will not need to worry about fiddling with it (hopefully). 


Before any code, I think it's useful to list some characteristics.

  • Creates a form at runtime in a thread and doesn't freeze your UI
  • Can have partial and overall progress bars shown plus window title, partial and overall textual progress
  • Automatically shows/hides partial progress (hides if partial progress is 0%)
  • Keeps ratio between window size and controls' size (window is sizeable)
  • Time out property. If not receiving any updates, asks the user whether to keep it open or close
  • Closes and disposes of itself automatically if the overall progress reaches 100% or if the caller Thread stops
  • Prevents the user from closing it
  • Can save user's preferred window size (you need to add a setting to your project --> progBarSize, System.Drawing.Size and then activate some commented out code from the class)


A screenshot of the demo project:


Using the code

The main class is too long to be shown here so instead of pasting it, it's better to describe its properties.

WindowTitle As String

OverallProgressValue As Int32 (1 to 100)

OverallProgressText As String (shown beneath the overall progress bar)

PartialProgressValue As Int32 (If omitted, the partial progress bar isn't shown)

PartialProgressText As String

CallerThreadSet As Threading.Thread
(WriteOnly, optional. The thread that called this class/thread. Use this if you want
 the progress bar window to close itself when the caller thread ends)

TimeOut As Int32
(Optional, default = 60. Time out in seconds without receiving any progress updates. User is asked  whether or not to close the window) 

This class creates a form which will have a timer that updates the controls at every tick.

Using the Class

The example below from the demo project uses the class to show the progress of a 10-second loop.

The milliseconds update the partial bar.

Each second updates the overall bar. 


Private Sub Task_2()
    Dim pb As New Progress_Bar
    pb.WindowTitle = "Task 2"
    pb.TimeOut = 60
    pb.CallerThreadSet = Threading.Thread.CurrentThread

    Dim dt As Date = Now
    Do While DateDiff(DateInterval.Second, dt, Now) < 10

        If Me.CheckBox1.Checked = True Then
            pb.PartialProgressText = "Partial Progress..."

            'Partial must have a value of at least 1% to be shown
            If Now.Millisecond < 100 Then
                pb.PartialProgressValue = 1
                pb.PartialProgressValue = ((Now.Millisecond / 999) * 100)
            End If

            pb.OverallProgressText = "Overall Progress..."
            pb.OverallProgressValue = ((DateDiff(DateInterval.Second, dt, Now) / 10) * 100)

            pb.OverallProgressText = "Overall Progress..."
            pb.OverallProgressValue = ((DateDiff(DateInterval.Second, dt, Now) / 10) * 100)

        End If

            'Activate the IF block below to test the scenario of the caller 
            'thread stopping before expected.
            'The progress bar will close itself after 5 secs.
            'If DateDiff(DateInterval.Second, dt, Now) > 5 Then
            'Exit Sub
            'End If

        'Loop only at every 1ms

    'Finish it. (here it will be 100% or over)
    If Me.CheckBox1.Checked = True Then pb.PartialProgressValue = 100
    pb.OverallProgressValue = ((DateDiff(DateInterval.Second, dt, Now) / 10) * 100)

End Sub


Points of Interest

You may also find useful the elements of multi threading and control creation at runtime .



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


About the Author

Software Developer at National Health Service
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHow can i use it for files copy Pin
ltdeta14-Jan-17 10:42
memberltdeta14-Jan-17 10:42 
QuestionHow to turn off use of PB in code Pin
Member 970648114-Nov-16 2:27
memberMember 970648114-Nov-16 2:27 
AnswerRe: How to turn off use of PB in code Pin
AlexF18518-Nov-16 5:35
memberAlexF18518-Nov-16 5:35 
GeneralMy vote of 5 Pin
Member 1083758826-Jan-16 14:33
memberMember 1083758826-Jan-16 14:33 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 15 Jul 2014
Article Copyright 2014 by AlexF185
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid