473,507 Members | 5,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to time a 'while' loop?

sueb
379 Contributor
I'm trying to implement a shut-down feature that exits all instances of my front end when I set a flag in the database. I've got it working pretty well, except that I want to put up a notice, and then wait 1 minute, and then quit the application.

I have the following:

- table Exit Now Request, comprising a single record with the Yes/No field Exit_Now
- a button on a menu that brings up the form Exit Now Request, which presents Exit_Now for modification
- form ExitingMessage, which just contains the "exiting in 1 minute" text (no "okay" button or anything)
- the following code (this code is in the On Timer event of form Detect Idle Time, which is opened by AutoExec and exists for the user's entire session--it monitors a user's activity, and quits the front end when the user has been inactive for more than the specified time (conIdleMinutes, which is 5, but is not referenced in this code). that all works great, thanks to this forum. this code is in that subroutine, but runs prior to the monitoring stuff.):

Expand|Select|Wrap|Line Numbers
  1.     Dim ExitWaiting
  2.  
  3.     ExitWaiting = 0
  4.     If Me.Exit_Now Then
  5.         DoCmd.OpenForm ExitingMessage
  6.         While (ExitWaiting / conTimerInterval) / 60 < conExitWait
  7.             ExitWaiting = ExitWaiting + conTimerInterval
  8.         Wend
  9.         Application.Quit acSaveYes
  10.     End If
  11.  
Let's see: conTimerInterval is set to 1000 (that's one second, right?); conExitWait is 1 (these both reside in the module that contains all the timing constants). The Timer Interval for Detect Idle Time is 1000.

I know I'm not handling the time correctly, because the ExitingMessage comes up, but then the quit happens immediately.

Can you help me untangle how I should be calculting that 1 minute of grace?
May 18 '11 #1
2 2775
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi Sue. The While loop in lines 6 to 8 executes just after you open the form. It will simply increment the counter ExitWaiting immediately, exit from the loop once the stated condition is reached, then quit the application, as you have found.

What I think you need to do is to increment the counter just once each time the timer event is triggered once you reach the "1-minute to go" mark. You will need to retain the value of variable ExitWaiting between calls to your timer event handler, and to do this you could either declare it globally within the form's code module or by declaring it as a Static variable.

Assuming a static declaration, with a 1-second timer interval your 'wait' code part within your event handler could look something like this:

Expand|Select|Wrap|Line Numbers
  1.     Static ExitWaiting as Long    
  2.  
  3.     If Me.Exit_Now Then
  4.         If ExitWaiting = 0 then
  5.             DoCmd.OpenForm ExitingMessage
  6.         End If
  7.         ExitWaiting = ExitWaiting + conTimerInterval
  8.         If ExitWaiting / conTimerInterval / 60 >= conExitWait then
  9.             Application.Quit acSaveYes
  10.         End If
  11.     End If
This just increments the static variable ExitWaiting by your timer interval on each pass through the timer event handler. The first time it does so, when ExitWaiting is 0, the notification form is opened. On the sixtieth increment (occurring after 1 minute has elapsed at a 1s timer interval), the last If condition should be satisfied and the Application.Quit should be triggered.

-Stewart
May 19 '11 #2
sueb
379 Contributor
Oh, Stewart, of course that's it! I was thinking in the sort of "procedural code" mode rather than in the "module mode".

Thanks for the nice code. I'll stick it in and try it tonight, but I'm going to mark you answer now--it's obviously the solution to my problem.
May 19 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

24
2666
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
1
1483
by: robin | last post by:
hi, i'm a newbie, but please bear with me for a second. i have this function inside a while-loop, which i'd like to loop forever, but i'm not sure about how to change the parameters of my...
7
3181
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
4
13131
by: Buckaroo Banzai | last post by:
Hi, I'm trying to generate 3 random numbers inside a while loop. The problem I encountered is that I always get the same 3 numbers even after I reseed the number using srand((unsigned)time(NULL));...
6
10792
by: mgcclx | last post by:
For loop and while loop. which one is faster? I see many articles fighting over it and different people come up with different results.
10
3259
by: BevG | last post by:
I would like to test a condition (using "if") on each recurrence of a while loop, but once the condition has been fulfilled the first time, it is bypassed for the rest of the while loop. eg. ...
9
3189
by: somenath | last post by:
Hi All, I have doubt regarding how compiler understands about while loop. For example the bellow mentioned code produce the output as mentioned bellow. #include<stdio.h> int main(void) {
3
2487
by: bmerlover | last post by:
I believe my problem lies inside the while loop. When I click the play button on the gui app, it goes inside the while loop, reads the file and calls the necessary function to do what it needs to do....
5
2209
by: =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?= | last post by:
Good evening all, I am trying to write a process that uses a while loop to cycle multiple files from an array throught the StreamReader Process. The whole thing works using: Dim...
3
1602
by: Brad | last post by:
Hi folks, I'm still fairly new to programming in python and programming in general. A friend of mine is in a CompSci 101 course and was working on a slider game when he encountered a problem. We...
0
7110
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...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7482
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...
0
5623
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,...
1
5041
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...
0
3191
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...
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.