Results 1 to 3 of 3

Thread: Program is pausing!!!

  1. #1

    Thread Starter
    Hyperactive Member Steve Stunning's Avatar
    Join Date
    Jul 1999
    Location
    Fairfax, Virginia
    Posts
    314

    Talking


    Need some ideas on how to step things up....

    I have a large amount of code in a timer that it ran every 1/2 second. It must be done this often...

    The problem I have is if I am moving the form, the timer kicks in and the program pauses. Same thing for when I am typing in a text box.

    How can I have that code run every 1/2 second w/o the pausing???


    Thanks in advance!!!
    Steve Stunning

  2. #2

  3. #3
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    in any loops in your code insert the line DoEvents, this lets VB handle everything that needs doing, and then continued with your code

    eg This is a bit of code that paints a parrern your form (if it's 255 pixels by 255 pixels and it's scalemoed is pixels, otherwise it's pretty useless)

    Code:
    For i = 1 to 255
    
        For j = 1 to 255
    
            Form1.PSet (i,j), RGB(255 * Rnd, i, j)
    
        Next j 
    
    Next i

    this takes a couple of seconds during which you can't move your form or click buttons

    if you insert the line DoEvents

    Code:
    For i = 1 to 255
    
        DoEvents
    
        For j = 1 to 255
    
            Form1.PSet (i,j), RGB(255 * Rnd, i, j)
    
        Next j 
    
    Next i


    then the code will run a little slower but you can move your form etc.

    Be carefull about doing this in a timer however, if your timer is set to 500 ms and the code takes 600 ms to operate then the first execution of the code will pause and wait for the second to start, then this execution will be interupted by the next and soon you will have hundreds of executions open and you will slowly run out of memory. If this code was in a timer I would do this

    Code:
    Private Sub Timer1_Timer()
    
    Static boolCodeRunning As Boolean
    
    Dim i As Integer
    Dim j As Integer
    
    If Not boolCodeRunning Then
    
        boolCodeRunning = True
    
        For i = 0 To 255
        
            DoEvents
            
            For j = 0 To 255
        
        
                Picture1.PSet (i, j), RGB(255 * Rnd, i, j)
        
            Next j
        
        Next i
    
        boolCodeRunning = False
    
    End If
    
    End Sub
    this prevents the code from running twice at the same time, if it hasn't finished then it just skips.

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