Thursday, March 7, 2013

Example ProgressBar in Visual Basic

Example ProgressBar in Visual Basic


Uses the ProgressBar to animate your program with actions such as stop, pause and resume his work are essential steps to good ProgressBar. Bad use of a ProgressBar can slow your program or make your program inaccessible.
I introduce two examples of ProgressBar in this article. The first one is a very simple case perfect for programmers who enjoy working in sequential programming as opposed to event-driven programming.


ProgressBar without Thread






''' <summary>
''' this is a sample on how to use a progress bar.
''' The main problem in this exemple is that the form is frooze while the progress bar is working.
''' </summary>
''' <remarks></remarks>
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Me.Text = My.Application.Info.AssemblyName


        With ProgressBar1
            .Style = ProgressBarStyle.Blocks
            .Step = 1
            .Minimum = 0
            .Maximum = 100
            .Value = 0
        End With

        With Button1
            .Text = "Start ProgressBar"
        End With

    End Sub

    Private Sub Button1_MouseUp(sender As System.Object, e As System.EventArgs) Handles Button1.MouseUp
        With ProgressBar1
            .Style = ProgressBarStyle.Blocks
            .Step = 1
            .Minimum = 0
            .Maximum = 100
            .Value = 0
        End With
        'if you repress the button now, it won't restart now
        Do
            'ALL events in your form will ahve to wait until this loop is completed!
            Threading.Thread.Sleep(100)
            ProgressBar1.PerformStep() 'can't use this if ProgressBar1.Style = ProgressBarStyle.Marquee

        Loop Until ProgressBar1.Value >= ProgressBar1.Maximum

        Me.Button1.Text = "finished, press to restart"

    End Sub
End Class




When you run the program and you press the button, the ProgressBar will animate. It works well because you could press the button a second time for the animation again. The value of the ProgressBar takes the value of zero and then increment at each stage of your programming work.

This example illustrates the perfect example of the most popular programming were computers were operating with a single processor 16-bit or Intel 80386, 80486, Pentium I, Pentium II, Citrix, AM386 or computer ... 32 bit until the 2000s. In fact, we were taught that the computer could only make a single statement at a time. And there was a time when the work of programmers was once measured by the number of lines of code written: 10000 lines, 50000 lines ...

What is interesting in this type of programming is that it is very popular not only in the programming software in computers but also in other applications such as electronic programming, industrial automation, engineering and more. For example, your dishwasher has a sequential behavior and has a sequence of wash cycles: closes the door, sprinkles, lather, rinse, dry and opens the door.


Programming events



In simple terms, the program must work in harmony with other competing programs and user. Program developed with events is the basic principle of the computer-aided design. This is obviously more difficult because the programmer first and foremost have a mentality contrary to selfishness. The programmer must think at all the elements around his program. For example, if the user wants to pause the program, nothing more frustrating than seeing the message "PROGRAM NOT RESPONDING" on the screen because the program is too busy to perform a certain task or because it was designed this way. Commercially speaking, then we can say that in 2013 the program was poorly designed.

Here is a code example of improved ProgressBar. In principle, it is almost the same as the previous example except that it is capable of handling user events with and be compatible with the resources of the computer.


 ProgressBar with Thread



''' <summary>
''' This is a sample of threading.
''' this sample have a PLAY, PAUSE, RESUME and STOP or Restart
''' Carefull, you can't use the ThreadState to check your Thread Status because is not reliable.
''' That is why a created a member called suspended.
''' </summary>
''' <remarks></remarks>
Public Class Form1
    Private tProgessBar As Threading.Thread
    Delegate Function tProgressBar_at_Max() As Boolean
    Private suspended As Boolean

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If tProgessBar Is Nothing Then tProgessBar = New Threading.Thread(AddressOf ProgessBarFunction)

        If tProgessBar.IsAlive Then
            If suspended Then
                suspended = Not suspended
               tProgessBar.Resume()
                Button1.Text = "Playing, click to pause"
            Else
                suspended = Not suspended
                tProgessBar.Suspend()
                Button1.Text = "Paused, click to resume"
            End If

        Else
            With ProgressBar1
                .Style = ProgressBarStyle.Blocks
                .Step = 1
                .Minimum = 0
                .Maximum = 100
                .Value = 0
            End With
            Button1.Text = "Playing, click to pause"
            tProgessBar = New Threading.Thread(AddressOf ProgessBarFunction)
            tProgessBar.IsBackground = True
            tProgessBar.Start()

            suspended = False
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Text = "Press to Start"
    End Sub

    Private Sub ProgessBarFunction()
        Do
            Me.Invoke(New MethodInvoker(Sub() Me.ProgressBar1.PerformStep()))

            Threading.Thread.Sleep(100)
        Loop Until ProgressBar_at_Max()
        Me.Invoke(New MethodInvoker(Sub() Me.Button1.Text = "finished, press to restart"))
    End Sub

    Private Function ProgressBar_at_Max() As Boolean
        ProgressBar_at_Max = False
        If ProgressBar1.Value >= ProgressBar1.Maximum Then
            ProgressBar_at_Max = True
        Else
            ProgressBar_at_Max = False
        End If

    End Function

End Class





First, the presence of a thread is important for the proper functioning of the programming events because you want to introduce a kind of subroutine in the main program which is our form. When you start the ProgressBar, the thread is constantly working, but in the background and becomes less important compared to Form1 and controls. We must also avoid falling into the room of the comparison. Many will think that a thread in the background is equivalent to the use of events as called Reactors or Events. Know that it is not at all the same thing. The mere fact that the level of experience required for the use of threads versus Reactors or Events should be enough to convince you.

 Note the use of the Resume function and suspends them which mean that the thread is suspended. During the time that the thread is suspended, the progress bar is virtually not using any resources from the processor.

You can download the program and source code examples used in this article.
Download a basic sample for the ProgressBar: progressBarSample.zip

Download more complex and interesting example of a ProgressBar: SampleProgressBarPause.zip





2 comments:

  1. up coming blog for vb
    http://l-visual-basic.blogspot.com

    ReplyDelete