Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Schedule a task programmatically

How can I schedule a task using delphi 7 like Google updater?
I'm not using the registry because it gets detected by Kaspersky antivirus as a false alarm.
Anything I add in the registry as a start-up item gets detected as a trojan so I decided to use task schedule

like image 406
user1023395 Avatar asked Jan 02 '12 12:01

user1023395


People also ask

How do I create a scheduled task script?

To create the scheduled task, type: Register-ScheduledTask -Action $action -Trigger $trigger -TaskPath "TASK-FOLDER" -TaskName "TASK-NAME" -Description "OPTIONAL-DESCRIPTION-TEXT". Press Enter. The scheduled task will then be created and will activate when the frequency and time triggers are reached.

How do you schedule a task in Java?

Java Timer schedule() methodThe schedule (TimerTask task, Date time) method of Timer class is used to schedule the task for execution at the given time. If the time given is in the past, the task is scheduled at that movement for execution.


1 Answers

The following piece of code shows how to delete and create the task which will run the application at system startup with system privileges. It uses the following command line:

However the Task Scheduler since Windows Vista supports force creation of tasks, I wouldn't use it for backward compatibility with Windows XP, where this flag doesn't exist. So the example below tries to delete the task (if already exists) and then create the new one.

It executes these commands:

schtasks /delete /f /tn "myjob"
schtasks /create /tn "myjob" /tr "C:\Application.exe" /sc ONSTART /ru "System"

/delete - delete the task
/f - suppress the confirmation
/create - create task parameter
/tn - unique name of the task
/tr - file name of an executable file
/sc - schedule type, ONSTART - run at startup
/ru - run task under permissions of the specified user

And here is the code:

uses
  ShellAPI;

procedure ScheduleRunAtStartup(const ATaskName: string; const AFileName: string;
  const AUserAccount: string);
begin
  ShellExecute(0, nil, 'schtasks', PChar('/delete /f /tn "' + ATaskName + '"'),
    nil, SW_HIDE);
  ShellExecute(0, nil, 'schtasks', PChar('/create /tn "' + ATaskName + '" ' +
    '/tr "' + AFileName + '" /sc ONSTART /ru "' + AUserAccount + '"'),
    nil, SW_HIDE);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ScheduleRunAtStartup('myjob', 'C:\Application.exe', 'System');
end;
like image 59
TLama Avatar answered Nov 08 '22 12:11

TLama