Results 1 to 6 of 6

Thread: [RESOLVED] Loop + Unload = Problem

  1. #1

    Thread Starter
    Hyperactive Member TupacShakur's Avatar
    Join Date
    Mar 2002
    Location
    Da Land Of Da Heartless...
    Posts
    493

    Resolved [RESOLVED] Loop + Unload = Problem

    Hello,
    I am using the following approach to do some simple animation inside a picture box:

    Code:
    Dim bFlag as Boolean
    
    Private Sub StartAnimation()
        bFlag = True
        Do While bFlag
            'Do Animation
            DoEvents
            Sleep 10 'update every 10 milliseconds
        Loop
    End Sub
    
    Private Sub StopAnimation()
        bFlag = False
    End Sub
    Now when i unload my form while the animation is on, i try to call the StopAnimation function, but that doesn't stop the animation, and my app is left hanging (it doesnt unload, but rather becomes invisible or something). In IDE mode, i have to press the "Stop" button. When compiled, the program can still be found in the apps list and the processes list of the task manager after i exit it while the animation loop is running.

    I am seeking some suggestions on how to deal with this matter WITHOUT RECURRING TO USING TIMERS FOR MY ANIMATION LOOP.

    Thank you.
    "And Now I'm Lika Major Threat, Cause I Remind U Of The Things U Were Made To Forget!" - (2PAC)

    "Now They Label Me a Lunatic, Couldn't Care Less, Death or Success is What I Quest, Cause I'm Fearless!" - (2PAC)

    " There's a light at the end of every tunnel, just pray it's not a train!! "



    I am 100% addicted to Tupac. What about you?
    I am 24% addicted to Counterstrike. What about you?
    The #1 Tupac Fans Web Site | The Official Tupac Web Site

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Loop + Unload = Problem

    I copied your exact code and I could not reproduce the problem. Is there any other code going on in your program that could cause it? Any timers?

    You should not have to do this, but you can try explicitly stating "if not bFlag Then Exit Do" right after the Do While bFlag line.

    But start a new VB project, and paste this code in there and run it (add a Command1 button to your form).

    This will help determine if it's something else in your code or not.

    Code:
    Option Explicit
    
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    Dim bFlag As Boolean
    
    Private Sub StartAnimation()
        bFlag = True
        Do While bFlag
            'Do Animation
            DoEvents
            Sleep 10 'update every 10 milliseconds
        Loop
    End Sub
    
    Private Sub StopAnimation()
        bFlag = False
    End Sub
    
    Private Sub Command1_Click()
        Me.Caption = "Animating"
        StartAnimation
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        StopAnimation
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member TupacShakur's Avatar
    Join Date
    Mar 2002
    Location
    Da Land Of Da Heartless...
    Posts
    493

    Re: Loop + Unload = Problem

    Well, my actual code differs in 2 ways from the code i posted above:

    1- There's the actual animation code, involving some simple drawing on a picture box, instead of the "'Do Animation" line.
    2- I had the DoEvents right after the "Do While" line, before the "Do Animation" part.

    Now i moved the DoEvents to in between the Animation part and the Sleep line. This solved my initial problem, but what happened was that my animation slowed down drastically for some reason.

    But the strangest thing i've ever seen in my programming years happened afterwards. I moved back the DoEvents line to where it was before, just before the animation code starts, knowing that the initial unload problem will reoccur, but expecting my animation to regain its speed (the same speed it had with the exact same code i reverted to). Big surprise, the animation stayed slow. I really can't figure this out, but it seems that after running my changed code for a few seconds, it seems that now i can't get my old code to run the way it was running before.. I am 100% positive that nothing else changed along the way, and that all possible internal and external factors are the same like before!!

    And to further explain my "speed" problem, let me just say that before i changed my code, i could alter the "Sleep 10" line to any value between 1 and 10 and get a noticeable change by the millisecond, with my animation running very fast at "Sleep 1" and fairly slow at "Sleep 10".
    But now, i can't seem to notice any change in animation speed for calling the Speed function with any value between 1 and 10 milliseconds, and the animation is running slow, for all 'Sleep' values, probably at the old 15 or 20 milliseconds speed.

    Any ideas on how did this happen and what to do about it?!!

    Thanks.
    Last edited by TupacShakur; Apr 5th, 2008 at 01:15 AM.
    "And Now I'm Lika Major Threat, Cause I Remind U Of The Things U Were Made To Forget!" - (2PAC)

    "Now They Label Me a Lunatic, Couldn't Care Less, Death or Success is What I Quest, Cause I'm Fearless!" - (2PAC)

    " There's a light at the end of every tunnel, just pray it's not a train!! "



    I am 100% addicted to Tupac. What about you?
    I am 24% addicted to Counterstrike. What about you?
    The #1 Tupac Fans Web Site | The Official Tupac Web Site

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Loop + Unload = Problem

    You could use the GetInputState API and only execute the DoEvents when there was an event to be serviced.
    Code:
    If GetInputState Then DoEvents
    Last edited by Doogle; Apr 5th, 2008 at 01:27 AM.

  5. #5

    Thread Starter
    Hyperactive Member TupacShakur's Avatar
    Join Date
    Mar 2002
    Location
    Da Land Of Da Heartless...
    Posts
    493

    Re: Loop + Unload = Problem

    Quote Originally Posted by Doogle
    You could use the GetInputState API and only execute the DoEvents when there was an event to be serviced.
    Code:
    If GetInputState Then DoEvents
    Yes, i did think about this, but from what i've tried, i had already concluded that this didn't have anything to do with the DoEvents call.

    Actually, i just managed to solve this problem, but i am still totally oblivious to what was the problem and how was it fixed !!

    What i just did was changing the AutoRedraw of the form containing my picture box to True (the picture box had AutoRedraw True initially, but even changing it to False didn't solve my new speed problem). Anyways, as i just said, what i did was changing the Form's AutoRedraw to True, and with the DoEvents in between the 'animation' and the 'sleep' code, all was working perfectly, speed and "unload hanging" wise.
    Now, knowing that i had the animation running at full speed before with the form's AutoRedraw set to False, i changed it back to False while keeping the DoEvents between the animation and the sleeping, and everything was still fine... I really can't understand what happened, but my observations would be:

    - Regarding the unload problem, i am now certain that it is related to the positioning of the DoEvents line. Placing it between the animation and the sleeping solves the hanging on unload problem.

    - Regarding the speed problem that ensued, it seems that it had nothing to do with changing the DoEvents line's position, but rather was some independent problem that coincided at the same time i altered the Animation/DoEvents/Sleep order. And it seems that it's some VB quirk or something cause toggling the form's AutoRedraw property seems to have solved this speed problem, even though i can now set the form's AutoRedraw to either True or False without any side effects.

    I welcome any input on this matter because by no means am i an expert in this area .
    Last edited by TupacShakur; Apr 5th, 2008 at 02:09 AM.
    "And Now I'm Lika Major Threat, Cause I Remind U Of The Things U Were Made To Forget!" - (2PAC)

    "Now They Label Me a Lunatic, Couldn't Care Less, Death or Success is What I Quest, Cause I'm Fearless!" - (2PAC)

    " There's a light at the end of every tunnel, just pray it's not a train!! "



    I am 100% addicted to Tupac. What about you?
    I am 24% addicted to Counterstrike. What about you?
    The #1 Tupac Fans Web Site | The Official Tupac Web Site

  6. #6

    Thread Starter
    Hyperactive Member TupacShakur's Avatar
    Join Date
    Mar 2002
    Location
    Da Land Of Da Heartless...
    Posts
    493

    Re: Loop + Unload = Problem

    Well, anyways, since there is no more additions to this issue, i'll mark this thread resolved.
    Thanks to all who replied.
    "And Now I'm Lika Major Threat, Cause I Remind U Of The Things U Were Made To Forget!" - (2PAC)

    "Now They Label Me a Lunatic, Couldn't Care Less, Death or Success is What I Quest, Cause I'm Fearless!" - (2PAC)

    " There's a light at the end of every tunnel, just pray it's not a train!! "



    I am 100% addicted to Tupac. What about you?
    I am 24% addicted to Counterstrike. What about you?
    The #1 Tupac Fans Web Site | The Official Tupac Web Site

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