从Windows服务注册任务计划程序

时间:2016-04-27 16:36:52

标签: c# windows windows-services scheduled-tasks

从Windows服务注册任务计划程序,但它无法正常工作。

using Microsoft.Win32.TaskScheduler;

服务内部编写的代码

public static void RegisterTask()
{
    using (TaskService ts = new TaskService())
    {
            TaskDefinition td = ts.NewTask();
            td.RegistrationInfo.Description = "Windows Service";

            td.Settings.AllowDemandStart = false;
            td.Settings.StartWhenAvailable = true;
            td.Settings.RunOnlyIfNetworkAvailable = true;

            var trigger = new TimeTrigger();
            trigger.Repetition.Interval = TimeSpan.FromMinutes(1);
            td.Triggers.Add(trigger);

            string exePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "svhostservice.exe");
            td.Actions.Add(new ExecAction(exePath, "Service", null));
            ts.RootFolder.RegisterTaskDefinition(@"Windows Service", td);

    }
} 

Windows服务是否无法注册任务计划程序?当我在EXE(WPF应用程序)中使用此代码时,它的工作正常。

0 个答案:

没有答案