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 do I run a VB program once every 24 hours? 3

Status
Not open for further replies.

SkennyR

Programmer
Mar 7, 2004
157
US
I posted this earilier, but I thought I would change the subject to maybe make it clearer as to what I want.

I need to run a small VB program every 24 hours, it will check a list of birthdates from a file, then if one of the dates matches the NOW date, this program will alert the user and start the bigger VB program that will allow user to look at the birthdates.
How can I easily do this? I know there has to be an easy way. Im new to this.
I would like to be able to control the times from the program itself.
 
Without using a scheduler, the program will need to run continuously and monitor the time. You can use a timer control to pause for a minute, then check the time and if you're at the proper time, kick off the main program. Otherwise back to sleep for another minute.

BTW, the NOW() function includes time. You probably want to use the Date() function when checking the birthdates.
 
Thanks Artie..
I like the idea of using the scheduler, but can I control the scheduler within the VB program?
Id like to give the user the option of scheduling the check times within the program.
Im new to VB6, and of course Im hungry for all the info I can get.
Thanks alot man, I really appreciate it..
--Ken
 
Windows has had a scheduler since NT 3.1 (I'm not counting the Win 9x series of OSes). Used to be you used the "AT" command line utility to set it's times, now you can use a control panel applet.

To use it, build your VB program to be a simple EXE (you can pass arguments via the command line if you want). Then set the Windows Scheduler to call it once a day.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
If you are using NT, Win2K or XP, you could use the scheduler via the AT command and create the command line in your program, then run it using the shell statement.

The scheduler is a little quirky - also it needs a user id and password to run the program. It doesn't appear the AT command uses that info - maybe it uses the login info of the current user.
 
How do we make the program run all the time (system tray!)

I like the idea of having it go to sleep, wake up every XX minutes, and then go back to sleep again.

How can that be done?

The more info the better.

Thanks guys...
 
I found this code on a VB site that will allow the program to "sleep" for a specified time. I have used it and it seems to work okay.

BTW, there's nothing magic about the system tray - that in itself doesn't make the program run all the time - it's just a convenient place to put the icon so it won't show up in the task list. Just don't exit your program and it will run forever!

'**************************************
'Windows API/Global Declarations for :Be
' tter Sleep Method
'**************************************


Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
'**************************************
' Name: Better Sleep Method
' Description:There are three major ways
' to implement a sleep in VB.
1. Set a timer and exit, let the timer restart you. This can involve setting up a little "state machine" that knows where to come back in. It is not difficult, just a hassle. A Select/Case/End Select is all that is needed, usually but requires some documentation about which section to put code in if changes are needed. It runs "enabled" so events are recognized.
2. Do a "busy loop" (do loop) which watches the clock and also does a DoEvents (optional) and exits on time passed. This also runs "enabled".
3. Call the Sleep API (Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)) This runs "disabled" for events.
The problem with the first one is the requirement of a state machine and documentation, the problem with the second one is that you burn CPU cycles that might be best used elsewhere (the CPU will be 100% busy forever), the problem with the third one is responsiveness of the application -- the app will not respond to anything while waiting for the API to release it back to the run queue.
Here is a compromise using the third method. I have just tested this and found it to be very efficient on a P500. The program has been running for 16 minutes and has not used 1 second of CPU time yet. That includes doing the I/Os (223 so far) to the networked disk to check for files. It uses the Sleep API but in very small increments of 100 msec per call. The DoEvents used to be needed to let other apps run. Now it is needed to let events happen inside your own app.
You get access to the app 10 times a second and the CPU utilization is very low. For my application (checking for a file on a networked disk and doing something with it), it is excellent in all respects. I have not tested this in an application where you would be waiting for messages in a time critical situation.
' By: Mike Morrow
'
'
' Inputs:The only input is the number of
' seconds to wait before exiting the subro
' utine.
'
' Returns:There are no return parameters
' .
'
'Assumes:This code requires kernel32.dll
' . It may run on kernel.dll but I cannot
' test that. It may not run on kernel.dll.
'
'
'Side Effects:There are no known side ef
' fects except, maybe, happiness.
'This code is copyrighted and has limite
' d warranties.
'Please see ' e.com/xq/ASP/txtCodeId.27347/lngWId.1/qx
' /vb/scripts/ShowCode.htm
'for details.
'**************************************

Private Sub Pause(iSecs As Integer)

Dim i As Integer

For i = 1 To iSecs * 10
Sleep 100
DoEvents
Next
End Sub
 
I'm sure I (and others)have done in previous threads in this forum. Have you tried a keyword search. If so, and you haven't found anything, drop another post in here and I'll see what I can do.
 
Thanks for all the help, this forum is neat. When I was doing Qbasic years ago I didnt have resources such as this.
I searched but dont really know what to search for, Strongm,
I would appreciate any help you can contribute, and it might be of value to someone else. I plan to post the code that I use when I get a working one. (I havent had a chance to try the suggestions from others, but I do plan to).
What is waitable object?
 
Well, we are potentially getting into some of the nitty gritty of the Windows API if we start playing with waitable (or synchronization) objects...are you sure you want to go there (since all the solutions previously posted should work)?
 
I thought that might be where you were going with your comment. I'd say skip it for now. Not something to throw at a "newbie" just yet - fur sure! :)
 
I have tried the routine that Artiechoke posted but I couldnt get it to work.
I wrote a routine that does what I need. Here it is:

I made a form and put a listbox on it (list1) and a timer, set for 1 minute.
Then I added this code:

dim a as string
dim b as string
dim d as string
Private Sub form_activate()

b = "12:32:00 AM"
d = "alarm is set for " + b
List1.AddItem d
End Sub

Private Sub Timer1_Timer()
a = Time
If Right(a, 2) = Right(b, 2) And Left(a, 5) = Left(b, 5)_ Then d = "Ok it happened at " + a: List1.AddItem d
End Sub

This displays the time I want to watch (b) in the list box and when that time is reached, the a message is added to the list box.
This works with the screen saver running too.

But to use this program the way I want, I will still have to either start it manually or manually put it in the start up folder.
I really would like to be able to do is run a program once every 24 hours, no need for it to run constantly. I know I can do this by placing the program in the task scheduler of windows. Can I easily do this from within my main VB program? This would alow the user of my program to either check "enable alerts" or uncheck it.
Thanks..
 
In that case you might want to check out the NetScheduleJobAdd API call
 
Thanks for the info, I looked up NetScheduleJobAdd on VB's help files, and it looks like this is for use on networked computers?? I plan to use this program on a personal computer, no need to be networked, Im running windows XP.
Will the NetScheduleJobAdd work on non-networked XP?
 
I rally hate to carry this thread out this far, but could you please give me an example using NetScheduleJobAdd?
Thanks..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top