Results 1 to 12 of 12

Thread: Simple Loop Question

  1. #1

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    I figured out all about DoEvents yesterday.

    I'll try to explain:

    Code:
    Private Sub DoSomething()
       Text1 = "working"
       Do Until Text1 = "Quit"
            DoEvents
       Loop
       Text1 = "Ready"
    End Sub
    
    Private Sub Command1_Click()
        DoSomething
    End Sub
    
    Private Sub Command2_Click()
        Text1 = "Quit"
        Do Until Text1 = "Ready"
            DoEvents
        Loop
        MsgBox "Hello"
    End Sub
    It never gets out of the Do..Loop in Command2.

    Any suggestions to fix this or even a different thought process would be greatly appreciated.

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Looks like you haven't quite got what the use of doevents is - look at this thread : http://forums.vb-world.net/showthrea...threadid=55745

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I think your code is in a kind of catch 22 situation.
    Try this code :

    Code:
    Private Sub DoSomething()
       Text1 = "working"
       Do Until Text1 = "Quit"
            DoEvents
       Loop
       Text1 = "Ready"
       MsgBox "Hello"
    End Sub
    
    Private Sub Command1_Click()
        DoSomething
    End Sub
    
    Private Sub Command2_Click()
        Text1 = "Quit"
    End Sub
    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Member
    Join Date
    Jan 2001
    Location
    Dublin
    Posts
    35
    What are you trying to do?

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'Use a boolean variable to stop a For Next Loop
    
    Option Explicit
    
    Dim bGetOut As Boolean 'trigger to stop loop
    
    Private Sub Command1_Click()
    bGetOut = False 'reset if you want to start again
    Dim myInt As Integer 'incremen
    
    'display a random number as form caption in a loop
    
    Do While Not bGetOut
    Randomize 'Rnd Function
    myInt = myInt + CInt(Rnd * 10) + 1 'create new caption
    Me.Caption = myInt
    DoEvents
    Loop
    End Sub
    
    Private Sub Form_DblClick()
    'dbl click the form to stop the loop
    bGetOut = True
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    Both of the examples that were given is basically the same as my original code w/o the Do..Loop in Command2.

    The only problem with that is I haven't verified that I'm out of the other Loop before I move on to something else.

  7. #7
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Well the example you gave is a little silly ...
    Give us something more substantial and we'll see.

    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  8. #8

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    I agree, it was silly but the structure was understood.

    What it's actually doing is the user keys in a stock symbol and then the program begins to download a bunch of information for that stock which takes a while. In case the user changes their mind and keys in another stock I want to cancel grabbing all the previous stock info and start downloading the new stock info.

  9. #9
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    I think someone posted this kinda thing already in here.
    However,

    try the following ...

    Code:
    Private Sub DownloadData_Click()
        Winsock1.Connect ...
    End Sub
    Private Sub Winsock1_Connect()
        Do While DoLoop
            'Download commands ...
        Loop
    End Sub
    Private Sub StopDownload_Click()
        DoLoop = False
    End Sub
    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  10. #10
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    the example was just to give you somthing to play with
    Code:
    Dim bReady As Boolean
    
    Private Sub DoSomething()
        Do While bReady = False
        Me.Caption = CInt(Rnd * 120)
            DoEvents
       Loop
    End Sub
    
    Private Sub Command1_Click()
        DoSomething
    End Sub
    
    Private Sub Command2_Click()
           Do While bReady = True
            DoEvents
        Loop
        MsgBox "Hello"
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  11. #11

    Thread Starter
    Lively Member new_money's Avatar
    Join Date
    Jan 2001
    Posts
    127
    HeSaidJoe, you reference bReady a few times in your example but you never set it or change it. How do any of the loops exit?

  12. #12
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    Dim bReady As Boolean
    
    Private Sub DoSomething()
        Do While Not bReady
        Me.Caption = CInt(Rnd * 1200)
        'reset in here when you get whatever you are looking for
        If Me.Caption = 1000 Then
           bReady = True
           Command2_Click
           Exit Do
         End If
            DoEvents
        Loop
        
    End Sub
    
    Private Sub Command1_Click()
        bReady = False
        DoSomething
    End Sub
    
    Private Sub Command2_Click()
        bReady = True
        MsgBox "Hello"
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

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