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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting up a scheduled task in windows

Status
Not open for further replies.

clientuser

Programmer
Apr 18, 2001
296
US
can anyone give me an example of how to setup a scheduled task in windows? i want to be able to setup task outside of my application in the windows scheduler.

also, im assuming the code would work in xp, 2000, 98? or is the scheduler different in the OS's?

if it doesnt work, what suggestions does everyone have to handle reminders if a user starts their computer but doesnt start the application that set the reminders.

examples would be greatly appreciated.
 
i actually found a good example of setting the schedule, however i am having trouble figuring out how to set it based on a date the user puts in:

Code:
vbScheduleJob "notepad", DateAdd("n", 1, Now), JOB_ADD_CURRENT_DATE

i tried to change the now in the code above, but it didnt set it to the date i put in.

any suggestions?
 
You probably need to tell us what vbScheduleJob is and what arguments it takes.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
this is the vbschedule code that i found on tek-tips:

Code:
Private Declare Function NetScheduleJobAdd Lib "netapi32.dll" (ByVal Servername As String, Buffer As Any, JobID As Long) As Long

Private Type AT_INFO
    JobTime As Long     ' The time is the local time at a computer on which the schedule service is running;
                        ' it is measured from midnight, and is expressed in milliseconds but is only accurate to the minute
    DaysOfMonth As Long ' bit for each day of month
    DaysOfWeek As Byte  ' bit for each day of week
    Flags As Byte       ' See enums
    Command As String   ' Unicode command string you want to run
End Type

Private Enum JobAdd
    JOB_RUN_PERIODICALLY ' If you set this flag, the job runs, and continues to run, on each
                         ' day for which a corresponding bit is set in the DaysOfMonth or
                         ' DaysOfWeek member. The job is not deleted after it executes.
                         ' If this flag is clear, the job runs only once for each bit set in
                         ' these members. The job is deleted after it executes once.
                         
    JOB_ADD_CURRENT_DATE ' If you set this flag, the job executes at the first occurrence of
                         ' JobTime at the computer where the job is queued.
                         ' Setting this flag is equivalent to setting the bit for the current
                         ' day in the DaysOfMonth member.
    JOB_NONINTERACTIVE
End Enum

Private Enum sjWeekdays
    Monday = 1
    Tuesday = 2
    Wednesday = 4
    Thursday = 8
    Friday = 16
    Saturday = 32
    Sunday = 64
End Enum

Private Enum sjDays
    d1 = 1
    d2 = 2
    d3 = 4
    d4 = 8
    d5 = 16
    d6 = 32
    d7 = 64
    d8 = 128
    d9 = 256
    d10 = 512
    d11 = 1024
    d12 = 2048
    d13 = 4096
    d14 = 8192
    d15 = 16384
    d16 = 32768
    d17 = 65536
    d18 = 131072
    d19 = 262144
    d20 = 524288
    d21 = 1048576
    d22 = 2097152
    d23 = 4194304
    d24 = 8388608
    d25 = 16777216
    d26 = 33554432
    d27 = 67108864
    d28 = 134217728
    d29 = 268435456
    d30 = 536870912
    d31 = 1073741824
End Enum

Private Function vbScheduleJob(strCommand As String, sjTime As Date, AddFlags As JobAdd, Optional DayOfWeek As sjWeekdays = 0, Optional DayOfMonth As sjDays = 0, Optional PCName As String = vbNullString) As Long
    Dim myInfo As AT_INFO
    Dim JobID As Long
    
    'set task example:
    'vbScheduleJob "notepad", DateAdd("n", 1, Now), JOB_ADD_CURRENT_DATE
    
    myInfo.Command = StrConv(strCommand, vbUnicode)
    myInfo.Flags = AddFlags
    myInfo.JobTime = DateDiff("s", "00:00:00", Format(sjTime, "hh:mm:ss")) * 1000
    myInfo.DaysOfWeek = DayOfWeek
    myInfo.DaysOfMonth = DayOfMonth
    
    NetScheduleJobAdd PCName, myInfo, JobID
    
    
    vbScheduleJob = JobID
    
End Function

with calling the function like:

Code:
vbScheduleJob "notepad", DateAdd("n", 1, Now), JOB_ADD_CURRENT_DATE
 
just to add to this thread, i was messing around with the code above and did this:

Code:
vbScheduleJob App.Path & "\test.exe", DateAdd("n", 1, Now), JOB_ADD_CURRENT_DATE

and the status each time on the scheduler is cannot run. even though the test.exe is in the correct location.

if i change it back to to say the original line of code that has notepad, it runs fine.

now im really confused.
 
Dude... the easiest way to do this is to use the AT scheduler.

From a comamnd prompt type AT /? and it will give you all the parameters etc that it takes. It can then be run through VB using the SHELL command.

Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
correct me if im wrong, but doesnt the service (prog) need to be running in order for the AT command to work?

because it says: the schedule service must be running to use the AT command....

 
Yes the windows scheduler service needs to be running.

You were saying that it works with Notepad but not with your program.

You can verify what it entered as data and whether there were problems.

For the AT scheduler (command prompt) type [AT], that lists the scheduled jobs. Find the one you scheduled and type [AT 3] or whatever number your job is and it will give details.

If it's not in the AT scheduler it will be in scheduled tasks. Navigate in Windows explorer to %WINDOWSDIR%\tasks\ and look up your task there. You can also see the properties through the right click menu. This will let you know if the location got set properly.

Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
this whole scheduling thing is really confusing for me.

all i am wanting to do is when a user wants to be reminded of a task they put into my application, it will setup a task outside of the application (so they dont have to worry about whether it will remind them or not if the app isnt running) and each time their computer starts the scheduler will run as necessary.

is there a better way to handle this.. any suggestions are greatly appreciated!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top