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!

How would you set up a program to close itself at a specific time

Status
Not open for further replies.

CubeE101

Programmer
Nov 19, 2002
1,492
US
How would you set up a program so that it will terminate if it is still running at a specific time (of day)...

Say like at 7:00 PM

I have a program that I keep on our network...
People usually run the program straight from the network drive so when I update the program, they always run the newest version...

The problem is that if someone leaves for the day and the program is still running, I can not replace it...

So... I want to set it up to close at a specific time, unconditionally, so I can replace the file...

I am assuming I can use either the Time function or the Timer Function to find out what time it is and compare to the preset termination time...

How could I set up the code to constantly check the time without wasting resources...

Could you set up an event?

Or would I be better off setting up a Timer control and having it timeout after the program is inactive for a period of time (like 30 or 60 minutes)

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif

 

Perhaps the easiest way other than the timer control is the SetTimer API...
[tt]
Public Declare Function SetTimer Lib "user32" Alias "SetTimer" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
[/tt]

At program start you could check the current time and find the difference. If greater than what the parameters expect then you could keep calling until the desired time.

If you search this site for "SetTimer" in your forums you should find about 23 or so threads with some execellent info.

Good Luck

 
Hi CubE101,

Can't say I've used the SetTimer function before; but I have used TIMER and TIME.

If the person leaves it running have the TIMER on the main form to check say every 2 minutes or so, and under the timer's code do the TIME comparison--somewhat similar to QB--'if time=7:00 then end else doevents endif'.

Good luck,
--MiggyD
 
I allso have my last version of the program on the server in a shared directory. But to prevent locking by any user, I don't let them run the program of the server, but I use a batch-file (remember DOS?) to copy the exe-file from the server IF a newer version than the current is found.
That way I can change the program whenever I want, and when a user starts the program, through the batch-file that is, I'm sure he/she runs the last version I put in the production environment.
If some user doesn't shut down his/her program that's too bad for him/her, but the others allways have the latest version ;D
The batch-file is running minimized.
example of content:

<---- runapp.bat ---->
Code:
@echo off
cls
xcopy \\server\progdirectory\myapp.exe c:\progra~1\localdirectory\ /D /Y
start c:\progra~1\localdirectory\myapp.exe
<---- end of batch-file ---->

Merlin is the name, and logic is my game...
 
Oh yes, don't forget to set the batch-file properties to close the window when finished. Otherwise you end up with some instances of the batch-file, all ended, on your taskbar.

Merlin is the name, and logic is my game...
 
OK...

I got it to work like this...

Timer1 is a timer control with interval set to 300000
(5 minutes * 60 seconds * 1000 miliseconds)

-----------------------------
'Set TimeOut to 7:30 pm (19:30)
Const TIMEOUT = 19 * 3600 + 30 * 60
'Set TimeIn to 7:45 pm (19:45)
Const TIMEIN = 19 * 3600 + 45 * 60

Private Sub Timer1_Timer()
If Timer > TIMEOUT And Timer < TIMEIN Then End
End Sub

-----------------------------

Or if you want to notify the user that the program is closing...

-----------------------------
'Set TimeOut to 7:30 pm (19:30)
Const TIMEOUT = 19 * 3600 + 30 * 60
'Set TimeIn to 7:45 pm (19:45)
Const TIMEIN = 19 * 3600 + 45 * 60

Private Sub Form_Load()
If Timer > TIMEOUT And Timer < TIMEIN Then End
End Sub

Private Sub Timer1_Timer()

'Close at TimeOut
If Timer > TIMEOUT And Timer < TIMEIN Then End
'Shorten interval at 5 minute mark...
If Timer > TIMEOUT - 300 And Timer < TIMEIN Then Timer1.Interval = 1000
'Warn of Termination at 1 minute mark...
If Timer > TIMEOUT - 60 And Timer < TIMEIN Then Label1 = &quot;Program will close in &quot; & Int(TIMEOUT - Timer) & &quot; seconds&quot;
End Sub

-----------------------------

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif

 
There could be other forms open...

I guess I could, but I was just testing it in a Test Application...

I have not yet applied it to my program yet...

When I do, I will do further testing to see which method works best...

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif

 
Yes, Vampire, In my target application, the termination code looks like this

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
mqlStop
End
End Sub


Due to the fact I need to disconnect from our PDM system...
So I will be using Unload Me...

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif

 
Sorry, I guess that it's one of my &quot;Hot Buttons&quot; issues that I dislike to see in a program. [smile]

Robert
 
I agree...

End is more or less a command that rolled over from the previous versions of basic, such as qbasic and GWBasic...

You can do much more with the Unload command...
One of the most popular features of it, is that it triggers the QueryUnload command so you can make sure that everything that needs to be done before ending the program, such as saving modified data, or disconnecting from dBase's, etc... is done before actually ending the program...

So if you get in the habbit of using Unload Me you can save yourself some time as opposed to using End commands throughout your program and then later changing your mind to use Unload, and having to replace every End command in your app...

But, old habbits die hard ;-)
And I still do some QB coding, just for fun, from time to time...

So you might occasionally see me using End in examples from time to time, especially if I was working with a Test Dummy App... ;-)

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif

 
If Time > CDate(Format(&quot;7:30:00 PM&quot;, &quot;hh:nn&quot;)) Then
'we are going to close
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top