Results 1 to 21 of 21

Thread: Stuck in a loop...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124

    Unhappy Stuck in a loop...

    I'm having a little trouble with a loop. When the user clicks the start button, it is supposed to draw a series of lines until the user clicks stop or it hits the far "wall".

    It does stop when it hits the edge, but the outside input doesn't seem to be getting through. I threw in a DoEvents statement at the beginning of the loop, but still no luck.

    I figure it's just something dumb, but any help would be greatly appreciated. Thanks.
    On Error Resume Screaming...

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    how is the "stop" button telling the loop to break? Like this? :

    VB Code:
    1. Dim stopLoop As Boolean
    2.  
    3.   Do Until x=5 'whatever you use
    4.     DoEvents
    5.     If stopLoop = True Then
    6.       Exit Do
    7.     End If
    8.    
    9.     'do whatever
    10.   Loop
    11.  
    12. Private Sub cmdStop_Click()
    13.   stopLoop = True
    14. End Sub

    like that?

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124
    Here's what I've got:

    Private Sub cmdStart_Click()
    If cmdStart.Caption = "&Start" Then
    Dim Drawing As Boolean
    Drawing = True
    cmdStart.Caption = "&Stop"
    Do While Drawing = True And cmdStart.Caption = "&Stop"
    DoEvents
    DoEvents
    LineY1 = 30 * Cos(LineX / 10)
    LineY2 = 30 * Cos(LineX / 15)
    If chkLine1.Value = 1 Then Dot StartX + LineX, StartY + LineY1, vbRed
    If chkLine2.Value = 1 Then Dot StartX + LineX, StartY + LineY2, vbYellow
    If chkResult.Value = 1 Then Dot StartX + LineX, StartY + LineY1 + LineY2, &H80FF&
    LineX = LineX + 1
    If LineX > picWave.ScaleWidth Then
    Drawing = False
    Exit Do
    End If
    If Not Drawing Then Exit Do
    Loop
    Else
    Drawing = False
    cmdStart.Caption = "&Start"
    End If
    End Sub

    Hopefully you can make sense of that. I really need the loop to run but still allow my user input, so those check boxes actually do something.
    On Error Resume Screaming...

  4. #4
    sunnyl
    Guest
    I don't think theres any need to actually put in two checks in the loop. You'll just be increasing the chance of making a logic error

    VB Code:
    1. Private Sub cmdStart_Click()
    2.     If cmdStart.Caption = "&Start" Then
    3.         cmdStart.Caption = "&Stop"
    4.         Do While cmdStart.Caption = "&Stop"
    5.             DoEvents
    6.             'drawing code
    7.             If HitTheWall = True Then
    8.                 cmdStart.Caption = "&Start"
    9.             End If
    10.         Loop
    11.     Else
    12.         cmdStart.Caption = "&Start"
    13.     End If
    14. End Sub

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124
    Um, I think I was unclear. The loop does exit when line hits the wall; that part works fine. The problem is I want the loop to allow user input while it is executing, so that the user can cancel the loop whenever they want to.

    Oh, and I knew I didn't need the double check, but I put it there because nothing else was working.
    On Error Resume Screaming...

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124
    Somebody has to know!
    On Error Resume Screaming...

  7. #7
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Well,

    I encountered a similar problem once, but what it was was there was a lag in the picture drawing commands and the actual picture
    update. So, when I selected a button to trigger a control variable to stop the picture processing, it appeared that my picture processing kept going for a while, then it stopped. Now, my picture drawing procedure did not have a self stop check built in, so it did stop when the trigger changed, it just took a while.

    So, in essence, I think your procedure kills itself, but the picture is still refreshing, so you think it still is active, and when you press the button to stop it, it's too late.


    Just as a test, throw in a msgbox command where your button triggers the stop.

    Change:

    Drawing = False
    cmdStart.Caption = "&Start"
    End If

    to:

    Drawing = False
    cmdStart.Caption = "&Start"
    msgbox "Well, your Trigger activated, anyway"
    End If

    And let me know what happens.

    -Lou

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124

    Well...

    I did what you said, and the msgbox popped up, but not until the loop had ended 5 seconds later because it exited itself when the drawing hit the wall.

    I decided to try it a few more times and sometimes the msgbox would pop up a second after I clicked the button and actually end the loop, sometimes it would wait 2 seconds then end the loop, sometimes not at all. I'm looking for pretty much instantaneous reaction, as would happen normally. (By normally, I mean if the loop weren't running).

    I think I'm going to try putting the drawing in a sub and having to different buttons.
    Last edited by DarkJedi9; Jun 25th, 2001 at 09:02 AM.
    On Error Resume Screaming...

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124

    You know what...

    Maybe I've been asking the wrong question.

    If you guys (and girls) wanted to have something looping in the backgroung and still allow user input, how would you do it? I've already tried a timer, which works but too slowly, so don't bother with that unless you know how to make them work faster.
    On Error Resume Screaming...

  10. #10
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    I'd like to do some investigating, but...

    I've recreated your code with form & controls, However, now I
    need to know a couple of things:

    DOT: Obviously a user defined function, maybe a variation of picWave.line?

    StartX, StartY, LineX, LineY1, LineY2: Are they Public type
    variables, and if so, what are their initial values?

    -If I can get these, I'll play with it and see if I can help you out.

    -Lou

  11. #11
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819
    I'd go with something similar to what Sunny gave you:
    VB Code:
    1. Dim blnStopLoop As Boolean
    2.  
    3. Private Sub SubTestLoop()
    4.     blnStopLoop = False
    5.     Do While blnStopLoop = False And x <= 5
    6.         ' Run your code
    7.         DoEvents
    8.     Loop
    9. End Sub
    10.  
    11. Private Sub cmdStop_Click()
    12.     blnStopLoop = True
    13. End Sub
    If your loop takes too long to run, test for the boolean in a couple of places in the code with a DoEvents and an Exit Do
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124
    NotLKH-

    Dot is a simple function that takes a point and then draws a three by three square of the given color around it. Basically a setpixel function that sets a bigger pixel.

    StartX, StartY are public in the form module, with StartX being 0 and StartY being half of the pixels scaleheight. They basically set the origin of the waves (that's what I'm drawing).

    LineX, LineY1, and LineY2 are also public in the form module. LineX is the current X position of the three waves I'm drawing. They all use the same X coordinate and it is incremented by 1 each time through the loop. LineY1 and LineY2 are the Y coordinates (in relation to my origin, not 0,0) of first two waves. They are found with a couple of slightly different sin(LineX) statements. The third wave gets its Y coordinate from the sum of LineY1 and LineY2.

    That what you need?
    On Error Resume Screaming...

  13. #13
    Helger
    Guest
    Maybe it doesn't matter but IMO it could:

    try it duncan-style: dont put all the code into your button event procedure but rather make the event procedure call the drawing one.
    (If all else fails try making two buttons on top of each other for a test to make isolating the problem easier and make one button visible and active while the other is not - THAT shouldn't make a difference to the above approach but sometimes VB does things that it shouldn't )

    hih

    helger

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124
    Thanks for the input, but I tried that, and it doesn't help. The problem is in the method of looping, I think, not the buttons.
    On Error Resume Screaming...

  15. #15
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Is your program something like this?



    Code:
    Dim StartX As Long
    Dim StartY As Long
    
    Private Sub cmdStart_Click()
    If cmdStart.Caption = "&Start" Then
    Call picWave_Click
    Dim Drawing As Boolean
    Drawing = True
    cmdStart.Caption = "&Stop"
    Do While Drawing = True And cmdStart.Caption = "&Stop"
    DoEvents
    DoEvents
    LineY1 = 30 * Cos(LineX / 10)
    LineY2 = 30 * Cos(LineX / 15)
    If chkLine1.Value = 1 Then Dot StartX + LineX, StartY + LineY1, vbRed
    If chkLine2.Value = 1 Then Dot StartX + LineX, StartY + LineY2, vbYellow
    If chkResult.Value = 1 Then Dot StartX + LineX, StartY + LineY1 + LineY2, &H80FF&
    LineX = LineX + 1
    If LineX > picWave.ScaleWidth Then
    Drawing = False
    Exit Do
    End If
    If Not Drawing Then Exit Do
    Loop
    Else
    Drawing = False
    cmdStart.Caption = "&Start"
    End If
    End Sub
    
    Private Sub Form_Load()
    StartX = 0
    StartY = picWave.ScaleHeight \ 2
    End Sub
    Private Sub Dot(ByVal MY_X As Long, ByVal MY_Y As Long, ByVal MY_C As Long)
    picWave.DrawWidth = 3
    picWave.PSet (MY_X, MY_Y), MY_C
    Form1.Caption = StartX & " " & picWave.ScaleWidth
    DoEvents
    
    End Sub
    
    Private Sub picWave_Click()
    picWave.Cls
    StartX = 0
    StartY = picWave.ScaleHeight \ 2
    End Sub
    Attached Images Attached Images  

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124

    Cool More or less, but...

    I don't do anything with the picWave_Click, although from the looks of it, that shouldn't change much, and your picture came out fine, so I'd say you pretty much got it.

    Oh, and my dot worked differently, it actually set nine pixels, but yours works better when I try it (less gaps) so I'll go with that!
    Last edited by DarkJedi9; Jun 25th, 2001 at 10:53 AM.
    On Error Resume Screaming...

  17. #17
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Glad you like it.

    Just wondering, the stop click works instantly on my machine.
    Does it work for you?

    -Lou

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124
    What are your stats?
    On Error Resume Screaming...

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124

    Talking Hey!!!

    Just found something that works: GetTickCount!

    If I use GetTickCount like this:
    Code:
    Do Until NextFrame <= GetTickCount
        DoEvents
    Loop
    NextFrame = NextFrame + (1000 / 40)  '1000 / frames per second
    Everything works. If I increase 40, it goes faster, but if I decrease it, user response is quicker. Woohoo!

    Well, thanks guys, although I still don't know why notLKH's worked on his machine. Oh, well.
    On Error Resume Screaming...

  20. #20
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    OOPS...

    the stop click works instantly on my machine

    heh,heh

    It worked instantly, but not on the program we were talking
    about. I was remembering it working on a development
    version that led up to my code post.

    It actually works, but not instantly. Its got some cumulative
    stall time.

    -Lou

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    124
    Yeah, that's what mine does. The higher the frame rate, the faster the drawing, and the slower the response, but it works well at about 30 fps, which is good enough for me.
    On Error Resume Screaming...

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