Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stop Endless Loop

Status
Not open for further replies.

007MCSE

IS-IT--Management
Jan 3, 2003
51
US
I have a continues loop That I am stopping using the Do While command.
The only problem is it quits my whole program. What do I need to change so that when I hit the Stop button (Command button on form) It stops the loop.

The Command button is labeled StopCmd_Click()

The code for the Loop is:-

Private Sub Timer1_Timer()
Static Temp As Integer
Temp = Temp + 1
Do While DoEvents()
If Temp Mod 2 = 0 Then Exit Do
Loop

Thanks for any help
Gary
 
?? Do While DoEvents() ??

That's the first time I've ever seen that before. What are you trying to accomplish here? What's the code inside the command button click event?

Robert
 
Nothing wrong with Do While DoEvents(). Perfectly legitimate.

However, I'd agree that we need to know the goal of the code and just what is in the COmmand button
 
strongm,

Does a "Do While DoEvents()" loop until the first time an event happens, regardless of what caused the event? Is that what it's for?

Robert
 
No, it loops whilst there is at least one visible form; DoEvents returns the current number of forms the application has visible.
 
I want a command button to exit the loop without exiting from the program.

I've tried the STOP command and that halts my whole program.

I'm looking for a way to stop the loop and set my form back so that its waiting for the user to select something else from within the form.
 
Ok,

Define a form level variable
private m_bStopPressed as boolean

Then
StopCMD_click()
m_bStopPressed = true
end sub

then the loop somewhere
Code:
Do While true      'crude but will loop forever
 temp = temp +1
 DoEvents()        ' Needed to ensure that Click event  
                   'processed
 if m_bStopPressed then Exit Do
loop

take care


Matt
 
Hi Matt

I did what you helped me with earlier on another problem.
Within the Stop comand button code, I re-opened my main form and that worked.

Here's the code:-

Private Sub Stop_Click()
Out PortAddress, &H0
Dim frmXmas1 As Xmas1
Set frmXmas1 = New Xmas1
frmXmas1.Show vbModal, Me
End Sub

Thanks again for your help
Gary

 
Why should there be a doevents inside a timer with a short interval and this short code? Is this the real code inside the loop? All the Best
Praveen Menon
pcmin@rediffmail.com
 
Yes thats the real code.
The timer is set for half a second, but is called up 24 times and then repeats. It is controlling 24 chanels of xmas lights.

Gary
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top