Results 1 to 6 of 6

Thread: Fastest way to give an update...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Cleveland, Ohio
    Posts
    263

    Post

    Ok, I have this program, and one of it's processes deals with for statements, and sometimes these statements can be looping for quite awhile. It's always nice to let the user know how far along the progress is, but the fact of the matter is that showing the progress slows down the progress of the loop... right now I'm using the Print Method in a picturebox, AutoRedraw is off, FontTransparent is False so that I don't have to clear the picturebox with new text. I find that in a standard loop of mine, same amount of loops for every test without DoEvents used, it takes and extra three seconds with progress reports than normal. That's a lot, considering that this test is a small test. What's the fastest way to give the user a progress report with the most minimal delay in the loop?

  2. #2
    Junior Member
    Join Date
    Apr 1999
    Location
    Bangalore, KA, India
    Posts
    18

    Post

    You can have a disabled timer control. Before the starting of the loop, enable the timer which shows the time in seconds in a label control. After the loop, disable the timer.

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    Why not use the ProgressBar control that is part of the Microsoft Windows Common Controls 5.0 or 6.0?

    ------------------
    Marty

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Cleveland, Ohio
    Posts
    263

    Post

    yes, my code is as fast as it can get for what it does, I used the progress bar already and it's much faster just because it updates in intervals, not every interval. I was just wondering what the fastest possible way for giving an update is, I guess the progress bar is it.

  5. #5
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Have you tried optimizing your code?
    The following progress bar adds an average of 2 seconds to a process..
    Code:
    Private Sub DoProgress(ByVal iValue As Integer, ByVal iMax As Integer)
        With Picture1
            If iValue = 0 Then
                .CurrentX = 0
                .Cls
            End If
            Picture1.Line (.CurrentX, 0)-((.ScaleWidth / iMax) * iValue, .ScaleHeight), , BF
        End With
    End Sub
    Example..
    Code:
    Private Sub Command1_Click()
        Dim iNum As Integer
        Dim tTimer As Single
        
        Picture1.ForeColor = vbBlue
        For iNum = 0 To 10000
            DoProgress iNum, 10000
            'Perform my Process..
            tTimer = Timer
            While (Timer - tTimer) < 0.001
            Wend
            'End of my Process
        Next
    End Sub
    
    Private Sub DoProgress(ByVal iValue As Integer, ByVal iMax As Integer)
        With Picture1
            If iValue = 0 Then
                .CurrentX = 0
                .Cls
            End If
            Picture1.Line (.CurrentX, 0)-((.ScaleWidth / iMax) * iValue, .ScaleHeight), , BF
        End With
    End Sub
    This only draws the part fo the Progressbar required, not the whole thing as the box upto that point is already blue.

    Another Option would be not to call the Progress bar on every interation of your Loop, calling it every other iteration would halve the time it took, eg.
    Code:
    Private Sub Command1_Click()
        Dim iNum As Integer
        Dim tTimer As Single
        
        Picture1.ForeColor = vbBlue
        For iNum = 0 To 10000
            If iNum Mod 2 Then DoProgress iNum, 10000
            'Perform my Process..
            tTimer = Timer
            While (Timer - tTimer) < 0.001
            Wend
            'End of my Process
        Next
    End Sub
    
    Private Sub DoProgress(ByVal iValue As Integer, ByVal iMax As Integer)
        With Picture1
            If iValue = 0 Then
                .CurrentX = 0
                .Cls
            End If
            Picture1.Line (.CurrentX, 0)-((.ScaleWidth / iMax) * iValue, .ScaleHeight), , BF
        End With
    End Sub
    In the end, it's not really a case of how much slower your progress bar makes the process, it's how it affects the Percieved Time, does the Process seem to go quicker with the Progressbar?

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]


    [This message has been edited by Aaron Young (edited 12-08-1999).]

  6. #6
    Member
    Join Date
    Nov 1999
    Posts
    63

    Post

    Are you updating your progress display with every iteration of your For loop? If this is the case, you may want to put an If statement around your progress display code that only allows the code to execute on every 10th, 100th or whatever iteration of the loop. This will cut down on the number of machine code instructions that must be processed to update the display. The amount of time required to test the If condition is miniscule when compared to the amount of time required to update the screen display.


    The following code will only execute the Debug.Print statement 10 times.

    Code:
    For n = 1 To 1000
    
        If n Mod 100 = 0 Then
            Debug.Print n
        End If
        
    Next
    Gerald M.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width