【问题标题】:Task scheduler trigger when system boot in C#在 C# 中系统启动时触发任务调度程序
【发布时间】:2012-05-31 11:49:48
【问题描述】:

我创建了一个任务调度程序并将其触发时间设置为固定,例如daily-5:00 pm,但我想在系统启动或引导时触发该事件。如果您有任何示例,请帮助我提供代码。

提前致谢。

代码 :--------------------------------------------- ---------

 public static void CreateTask()
        {
            using (TaskService task = new TaskService())
            {`enter code here`
                TaskDefinition taskdDef = task.NewTask();

                taskdDef.RegistrationInfo.Description = "Does something";
                taskdDef.RegistrationInfo.Documentation = "http://www.mysite.com";

                taskdDef.Settings.ExecutionTimeLimit = new TimeSpan(0, 10, 0);
                taskdDef.Settings.AllowDemandStart = true;

                taskdDef.Actions.Add(new ExecAction(@"D:\Myfolder\bin\SGSclient.exe", "yourArguments", null));
                task.RootFolder.RegisterTaskDefinition("YourTask", taskdDef);
            }
        }

【问题讨论】:

  • 创建一个windows任务并在系统启动时使用Task Scehduler启动它
  • 我也尝试过使用窗口服务。首先让我澄清一下,我有一个窗口应用程序并设置了该文件。之后我创建了窗口服务并在窗口服务中提供了该设置文件的路径。问题就在这里,当我为该窗口服务创建另一个设置时。因为我想像设置一样使用,所以可以安装在任何系统上。
  • 有this good library 帮助安排任务。我不写这个作为答案,因为我不知道它是否可以处理启动要求

标签: c# scheduled-tasks


【解决方案1】:

使用来自 GitHub 的 Task Scheduler Manager Library,您可以编写此代码

using System;
using Microsoft.Win32.TaskScheduler;

class Program
{
   static void Main(string[] args)
   {
      // Get the service on the local machine
      using (TaskService ts = new TaskService())
      {
         // Create a new task definition and assign properties
         TaskDefinition td = ts.NewTask();
         td.RegistrationInfo.Description = "Does something";

         // Create a trigger that will fire after the system boot
         td.Triggers.Add(new BootTrigger() );

         // Create an action that will launch Notepad whenever the trigger fires
         td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

         // Register the task in the root folder
         ts.RootFolder.RegisterTaskDefinition(@"Test", td);

         // Remove the task we just created
         ts.RootFolder.DeleteTask("Test");
      }
   }
}

【讨论】:

  • 谢谢史蒂夫,非常好的教程。
【解决方案2】:

您可以将其作为启动任务添加到注册表中。见here。

【讨论】:

  • 在有请求管理员权限的程序时不起作用
【解决方案3】:

2000,经验值

开始 -> 设置 -> 控制面板 -> 计划任务
打开任务的属性。
打开“计划”标签。
从“计划任务”下拉菜单中选择“在系统启动时”。

远景

开始 -> 设置 -> 控制面板 -> 管理工具 -> 任务计划程序
打开任务的属性。
打开“触发器”标签,然后编辑或创建触发器。
从“开始任务”下拉菜单中选择“启动时”。

【讨论】:

  • 如果客户必须自己完成工作,那也无济于事;)
【解决方案4】:

您应该创建一个Windows Service。

【讨论】:

  • 运行应用程序的服务?是不是有点过分了?此外,他可能(或不知道,我们不知道)有一些 UI。
  • 任务调度器中不可能吗?
  • 谢谢 Asif,我也尝试过使用窗口服务。首先让我澄清一下,我有一个窗口应用程序并设置了该文件。之后我创建了窗口服务并在窗口服务中提供了该设置文件的路径。问题就在这里,当我为该窗口服务创建另一个设置时。因为我想像设置一样使用,所以可以安装在任何系统上。
  • 我从你们的 cmets 得到的是你们不想要两个安装程序。我认为您可以制作应用程序和服务的单个安装程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-02
  • 2017-11-13
  • 2014-07-30
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
相关资源
最近更新 更多