|
-
Apr 4th, 2008, 04:38 PM
#1
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
-
Apr 5th, 2008, 01:10 AM
#2
Thread Starter
Hyperactive Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|