加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何以编程方式安排任务

发布时间:2020-12-15 04:21:00 所属栏目:大数据 来源:网络整理
导读:如何使用delphi 7(如Google更新程序)安排任务? 我没有使用注册表,因为它被卡巴斯基反病毒软件检测为虚警. 我在注册表中添加的任何启动项都会被检测为特洛伊木马,因此我决定使用任务计划 解决方法 以下代码显示了如何删除和创建将在系统启动时以系统权限运行
如何使用delphi 7(如Google更新程序)安排任务?
我没有使用注册表,因为它被卡巴斯基反病毒软件检测为虚警.
我在注册表中添加的任何启动项都会被检测为特洛伊木马,因此我决定使用任务计划

解决方法

以下代码显示了如何删除和创建将在系统启动时以系统权限运行应用程序的任务.它使用以下命令行:

但是,自Windows Vista以来,任务计划程序支持强制创建任务,我不会将其用于向后兼容Windows XP,因为Windows XP不存在此标志.
因此,下面的示例尝试删除任务(如果已存在),然后创建新任务.

它执行以下命令:

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

这是代码:

uses
  ShellAPI;

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

procedure TForm1.Button2Click(Sender: TObject);
begin
  ScheduleRunAtStartup('myjob','C:Application.exe','System');
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读