473,520 Members | 2,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

infinite loop

553 Contributor
Hi

The loop inside my code runs infinite. I couldn't figure out why.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command50_Click()
  2. Dim bFlag As Boolean
  3.     bFlag = False
  4.  
  5.    Do While (bFlag = False)
  6.       Me.timeDisplay = Time()
  7.  
  8.       If Me.txtHour = CStr(Hour(Time())) And _
  9.          Me.txtMinute = CStr(Minute(Time())) And _
  10.          Me.txtSecond = CStr(Second(Time())) Then
  11.          MsgBox "Some message"
  12.          bFlag = True
  13.       End If
  14.    Loop
  15.  
  16. End Sub
  17.  
I put in hour, minute, second in textboxes on form and comparing thier values with system time. If time matches, MsgBox will run.
The loop runs all the time - as if condition is never met ?

Can anyone help?
Thanks
Qi
Aug 22 '07 #1
5 4567
istya
35 New Member
Hi

The loop inside my code runs infinite. I couldn't figure out why.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command50_Click()
  2. Dim bFlag As Boolean
  3.     bFlag = False
  4.  
  5.    Do While (bFlag = False)
  6.       Me.timeDisplay = Time()
  7.  
  8.       If Me.txtHour = CStr(Hour(Time())) And _
  9.          Me.txtMinute = CStr(Minute(Time())) And _
  10.          Me.txtSecond = CStr(Second(Time())) Then
  11.          MsgBox "Some message"
  12.          bFlag = True
  13.       End If
  14.    Loop
  15.  
  16. End Sub
  17.  
I put in hour, minute, second in textboxes on form and comparing thier values with system time. If time matches, MsgBox will run.
The loop runs all the time - as if condition is never met ?

Can anyone help?
Thanks
Qi
Try displaying the values of Hours, Mins and seconds to see if they actually match.

Atlernatively, to stop you having a infinite loop, you can have a counter and when it reaches a value, exit the loop.

like
Expand|Select|Wrap|Line Numbers
  1. dim counter as integer
  2. dim value as integer
  3.  
  4. If Me.txtHour = CStr(Hour(Time())) And _
  5.          Me.txtMinute = CStr(Minute(Time())) And _
  6.          Me.txtSecond = CStr(Second(Time())) Then
  7.          MsgBox "Some message"
  8.          bFlag = True
  9. else
  10.         counter = counter +1
  11.         if counter = value then
  12.               bFlag = True
  13.         end if
  14. End If
Aug 22 '07 #2
questionit
553 Contributor
istya

its working now..

thanks

qi
Try displaying the values of Hours, Mins and seconds to see if they actually match.

Atlernatively, to stop you having a infinite loop, you can have a counter and when it reaches a value, exit the loop.

like
dim counter as integer
dim value as integer

If Me.txtHour = CStr(Hour(Time())) And _
Me.txtMinute = CStr(Minute(Time())) And _
Me.txtSecond = CStr(Second(Time())) Then
MsgBox "Some message"
bFlag = True
else
counter = counter +1
if counter = value then
bFlag = True
end if
End If
Aug 22 '07 #3
questionit
553 Contributor
Hi

There is one problem left. The same code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command50_Click()
  2. Dim bFlag As Boolean
  3.     bFlag = False
  4.  
  5.  
  6.     Do While (bFlag = False)
  7.  
  8.        Me.timeDisplay = Time()           <----- never displayed on Form
  9.  
  10.        If Me.txtHour = CStr(Hour(Time())) And _
  11.           Me.txtMinute = CStr(Minute(Time())) And _
  12.           Me.txtSecond = CStr(Second(Time())) Then
  13.  
  14.           MsgBox "Some message"
  15.           bFlag = True
  16.        End If
  17.  
  18.     Loop
  19.  
  20. End Sub
  21.  
When the above while loop is running, the first statement : Me.timeDisplay = Time() runs but its result is not shown on the Form because the form is disabled/inactivated while the loop runs. what is the solution to this problem?

thanks
Qi



istya

its working now..

thanks

qi
Aug 22 '07 #4
questionit
553 Contributor
I have sorted my problem out by just adding DoEvents within the Loop.

I have still 2 questions:

1- How can i stop that loop or exit from the Sub (Command50_Click()) on a Button Click

2- DoEvents uses 100% PC's resource, is there any alternative?

Code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command50_Click()
  2.  
  3.     bFlag = False
  4.  
  5.     Do While (bFlag = False)
  6.  
  7.        Me.timeDisplay = Time()          
  8.  
  9.        If Me.txtHour = CStr(Hour(Time())) And _
  10.           Me.txtMinute = CStr(Minute(Time())) And _
  11.           Me.txtSecond = CStr(Second(Time())) Then
  12.  
  13.           MsgBox "Some message"
  14.           bFlag = True
  15.        End If
  16.       DoEvents
  17.     Loop
  18.  
  19. End Sub
  20.  


Thanks
Qi

Hi

There is one problem left. The same code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command50_Click()
  2. Dim bFlag As Boolean
  3.     bFlag = False
  4.  
  5.  
  6.     Do While (bFlag = False)
  7.  
  8.        Me.timeDisplay = Time()           <----- never displayed on Form
  9.  
  10.        If Me.txtHour = CStr(Hour(Time())) And _
  11.           Me.txtMinute = CStr(Minute(Time())) And _
  12.           Me.txtSecond = CStr(Second(Time())) Then
  13.  
  14.           MsgBox "Some message"
  15.           bFlag = True
  16.        End If
  17.  
  18.     Loop
  19.  
  20. End Sub
  21.  
When the above while loop is running, the first statement : Me.timeDisplay = Time() runs but its result is not shown on the Form because the form is disabled/inactivated while the loop runs. what is the solution to this problem?

thanks
Qi
Aug 22 '07 #5
Rabbit
12,516 Recognized Expert Moderator MVP
This question has already been answered in another one of your posts a while back. This one will be closed.
Aug 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
5529
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a fallacy built on the assumption of mythical infinite all powerfull machines. In reality we deal with finite machines that are capable of two states in...
5
4914
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
4
2615
by: LOPEZ GARCIA DE LOMANA, ADRIAN | last post by:
Hi all, I have a question with some code I'm writting: def main(): if option == 1: function_a()
13
2268
by: Vector | last post by:
Is any infinite loop better than other? Is there any difference between there efficiency?
10
7622
by: Steven Woody | last post by:
i have a program which always run dead after one or two days, i think somewhere a piece of the code is suspicious of involving into a infinite loop. but for some reason, it is very hard to debug. so i thing there might be tool which can statically analysis the code and notice me this kind of potential problems if found. any suggestion? ( i...
44
4320
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
0
7313
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7222
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7181
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7577
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5758
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5141
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3290
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3287
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.