Scheduling a task executing each 1st sunday of the month ?

This forum is designated to discuss SiComponents Scheduling Agent.
Post Reply
WeirdAl1
Posts: 4
Joined: Mon Nov 13, 2023 3:39 pm

Scheduling a task executing each 1st sunday of the month ?

Post 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 :| )
isiticov
Site Admin
Posts: 2385
Joined: Thu Nov 21, 2002 3:17 pm

Re: Scheduling a task executing each 1st sunday of the month ?

Post 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.
Best regards,
Igor Siticov.
WeirdAl1
Posts: 4
Joined: Mon Nov 13, 2023 3:39 pm

Re: Scheduling a task executing each 1st sunday of the month ?

Post by WeirdAl1 »

Owww lovely. Thank You!
WeirdAl1
Posts: 4
Joined: Mon Nov 13, 2023 3:39 pm

Re: Scheduling a task executing each 1st sunday of the month ?

Post by WeirdAl1 »

Hmm.. tried it, but it throws some error "Months:".. which i am not sure what means.
isiticov
Site Admin
Posts: 2385
Joined: Thu Nov 21, 2002 3:17 pm

Re: Scheduling a task executing each 1st sunday of the month ?

Post 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;
Best regards,
Igor Siticov.
WeirdAl1
Posts: 4
Joined: Mon Nov 13, 2023 3:39 pm

Re: Scheduling a task executing each 1st sunday of the month ?

Post by WeirdAl1 »

Pfff. Lovely ! Thank You !
Post Reply