- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
procedure AddTask(const taskname:widestring; const filename:widestring);
begin
shellexecutew(0,'OPEN','schtasks.exe', pwidechar('/create /tn "\mytaskname)))" /tr "\"'+filename+'\"" /SC ONCE /ST 00:00 /RL HIGHEST /F'),nil,0);
end;
function FindTask(run:boolean=false):boolean;
function ParseCommand(text:string):string;
begin
Result:=parsestr('<Command>', text,'</Command>');
Result:=StringReplace(Result,'"','',[rfReplaceAll]); // AnsiExtractQuotedStr очень глюкавая.
end;
var
TaskService: ITaskService;
Folder: ITaskFolder;
Tasks: IRegisteredTaskCollection;
Task: IRegisteredTask;
RTask:IRunningTask;
i: Integer;
Path:string;
begin
result:=false;
if (Succeeded(CoInitialize(nil))) then
begin
if (Failed(CoCreateInstance(CLSID_TaskScheduler, nil, CLSCTX_INPROC_SERVER, IID_ITaskService, TaskService))
or (Failed(TaskService.Connect(Null, Null, Null, Null)))) then
begin
CoUninitialize();
TaskService := nil;
end;
end;
if not assigned(taskservice) then exit;
TaskService.GetFolder('\', folder);
if Folder <> nil then
begin
Folder.GetTasks(0, tasks);
if Tasks <> nil then
begin
if Tasks.Count > 0 then
begin
for i:=1 to Tasks.Count do
begin
Task:=Tasks.Item[i];
if Task <> nil then
begin
if widesametext(task.Name,AppTaskName) then
begin
Path:=ParseCommand(Task.XML);
if not widesametext(Path, ParamStr(0)) then
begin
folder.DeleteTask(task.Name,0);
break;
end;
if run then
begin
Task.Run('', rtask);
Sleep(5000); // задержка необходима, иначе обращение к Task обрушит прогу с сообщением "нет ни одного выполняющегося экземпляра задачи"
Result:=(rTask.State=TASK_STATE_RUNNING);
break;
end
else
begin
result:=true;
break;
end;
end;
Task:=nil;
end;
end;