dcsimg
Results 1 to 7 of 7

Thread: how to detect when FOR statement is done

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2019
    Posts
    142

    how to detect when FOR statement is done

    how can i check if FOR statement is done so that i can move on doing something else otherwise it will be on loop cycle without me knowing.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,995

    Re: how to detect when FOR statement is done

    The For statement is done when the code goes to the line after the Next statement.

    The normal operation is that nothing else gets done while the loop is running. The can of course be altered if you have doevents in that loop but that is not recommended. Timers can alter behavior and of course multi threading can have more than one code path running at the same time.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,563

    Re: how to detect when FOR statement is done

    Quote Originally Posted by doberman2002 View Post
    how can i check if FOR statement is done so that i can move on doing something else otherwise it will be on loop cycle without me knowing.
    OK... something is up.... this is not the first time I've seen this question ... not today though... but I have seen this before.. I'd swear... deja moo?


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,931

    Re: how to detect when FOR statement is done

    This is the first time I see it.

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,835

    Re: how to detect when FOR statement is done

    It's also the first time I've seen it.

    I think it's important to remember that VB6 is inherently single-threaded. Not even the timer will fire if we keep control of the thread. For example, when loaded, the following code fires a timer every 2 seconds (IDE or compiled). However, once we click the form, that timer will never fire again. (If we're compiled, the program will effectively be hung. If we're in the IDE, we can break out.)

    So, to my eyes, unless we've got a DoEvents in our loop (or we've spawned a second thread through some of those advanced processes), the loop is finished "when it's finished!"

    Just throw the following into Form1 to see the single-threadedness of VB6:

    Code:
    
    Option Explicit
    Dim WithEvents Timer1 As VB.Timer
    
    Private Sub Form_Click()
        Dim i As Long
        For i = 1 To 10
            i = i - 1
        Next
    End Sub
    
    Private Sub Form_Load()
        Set Timer1 = Me.Controls.Add("VB.Timer", "Timer1", Me)
        Timer1.Interval = 2000
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
        MsgBox Time
    End Sub
    
    
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  6. #6

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,620

    Re: how to detect when FOR statement is done

    if you are looking for a specific result during the for loop, you can use exit for, so the loop finishes when the result is found
    if this answer does not help i have misunderstood the issue, then more explanation might help

    generally for loops are limited and stop after however many iterations are specified at the start, so should not be endless, do loops normally run until some condition is met, so can more likely be endless
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width