Page 1 of 1
Scheduling a task executing each 1st sunday of the month ?
Posted: Tue Nov 14, 2023 9:12 pm
by WeirdAl1
Hello dear SiComponents.
I wonder is it possible with the task scheduler in the trial version to schedule a task which to run an executable each 1st Sunday of the month, and if possible - how ? ( since I really failing to find such properties somehow

)
Re: Scheduling a task executing each 1st sunday of the month ?
Posted: Wed Nov 15, 2023 2:15 am
by isiticov
Hello,
Here is the sample code:
Code: Select all
// start notepad.exe every 1st Sunday of the month at 09:00AM during 90 days
with Scheduler.CreateNewItem(Format('New task %d', [GetTickCount])) do
begin
// 2 lines optional necessary for Windows VISTA
CurrentAction := 1;
InsertAction(VISTA_TASK_ACTION_EXEC);
ApplicationName := 'notepad.exe';
Triggers.Add(ttMonthlyDow);
// create start time data
DateTimeToSystemTime(Now, ST);
ST.wHour := 9;
ST.wMinute := 0;
ST.wSecond := 0;
ST.wMilliseconds := 0;
{ or could be used the following
ST.wYear := YearOf(Now);
ST.wMonth := MonthOf(Now);
ST.wDay := DayOf(Now);
ST.wHour := 9;
ST.wMinute := 0;
ST.wSecond := 0;
ST.wMilliseconds := 0;
}
Triggers[0].StartTime := SystemTimeToDateTime(ST);
Triggers[0].BeginDate := Date;
Triggers[0].EndDate := Date + 90; // will be executed during 3 months
Triggers[0].HasEndDate := True;
Triggers[0].VistaMonthlyDOWWeeks := [tvmdowFirst]; // first week only
TriggerDetails.MonthlyDOW.wWhichWeek := 1; // first week only for non-Vista task
TriggerDetails.MonthlyDOW.rgfDaysOfTheWeek := 1; // for Sunday
Triggers[0].Details := TriggerDetails;
// trigger details must be changed only this way
// could be used UpdateTriggers and Save
Triggers.UpdateTriggers;
// to set-up a task for user w/o password
// use the code below
if (not Scheduler.RunningVistaOrLater) then
Flags := Flags + [tfRunOnlyIfLoggedOn]
else
VistaFlags := VistaFlags + [tfvNeedLogon{, tfvHidden}];
SetAccountInformation(GetOSUser, '');
// to setup a "system" task that will run under SYSTEM account
// use the line below
// SetAccountInformation('', '');
// if we want to run our task with highest access rights (like Admin)
// on Vista we must set RunLevel property like the following:
// if Scheduler.RunningVistaOrLater and IsUserAnAdmin then
// TaskDefinition.Principal.RunLevel := TASK_RUNLEVEL_HIGHEST;
Save;
// or just Deactivate(True)
//Deactivate(True);
end;
Hope this helps.
Re: Scheduling a task executing each 1st sunday of the month ?
Posted: Wed Nov 15, 2023 11:33 am
by WeirdAl1
Owww lovely. Thank You!
Re: Scheduling a task executing each 1st sunday of the month ?
Posted: Wed Nov 15, 2023 10:33 pm
by WeirdAl1
Hmm.. tried it, but it throws some error "Months:".. which i am not sure what means.
Re: Scheduling a task executing each 1st sunday of the month ?
Posted: Thu Nov 16, 2023 2:05 am
by isiticov
Sorry, forgot to add settings for the months of the year to run the task. Add the following code:
Code: Select all
TriggerDetails.MonthlyDOW.rgfMonths := 0;
for I := 0 to 11 do
TriggerDetails.MonthlyDOW.rgfMonths := TriggerDetails.MonthlyDOW.rgfMonths or 1 shl I; // for all 12 months
before the line
Code: Select all
Triggers[0].Details := TriggerDetails;
Re: Scheduling a task executing each 1st sunday of the month ?
Posted: Thu Nov 16, 2023 6:21 am
by WeirdAl1
Pfff. Lovely ! Thank You !