简体   繁体   English

是否可以在自己的应用程序中使用Windows 7任务计划程序

[英]Is it possible to use Windows 7 Task scheduler in own application

I'm developing add-on based application. 我正在开发基于附加组件的应用程序。 Every add-on use scheduler. 每个附加组件都使用调度程序。 Loaded add-on schedule task. 加载的附加计划任务。 My application run only one instance. 我的应用程序只运行一个实例。 Sometimes application closed, sometimes running. 应用程序关闭,有时运行。 Therefore i need to use Windows 7 Task scheduler 因此,我需要使用Windows 7任务调度程序

How to use task scheduler on own application? 如何在自己的应用程序上使用任务调度程序?

I need create new task from application 我需要从应用程序创建新任务
I need that when task finish, Send message to my application or invoke any function of my application 我需要在任务完成时,将消息发送到我的应用程序或调用我的应用程序的任何功能
Is it possible? 可能吗?
How to do it? 怎么做?

Check out this project at CodeProject. 在CodeProject查看这个项目。

A New Task Scheduler Class Library for .NET 一个新的.NET任务调度程序类库

If you want to interact with the Windows 7 Scheduled Tasks system from your code to create, manage, or delete a task that is no problem. 如果要从代码中与Windows 7计划任务系统进行交互,以创建,管理或删除没有问题的任务。 (I cover this in the Windows 7 course I wrote for Pluralsight.) Add a COM reference to TaskScheduler and you then do this sort of thing: (我在为Pluralsight编写的Windows 7课程中介绍了这一点。)向TaskScheduler添加一个COM引用然后你做了这样的事情:

ITaskService scheduler = new TaskSchedulerClass();
scheduler.Connect(null, null, null, null);

ITaskFolder rootFolder = scheduler.GetFolder("\\");
ITaskFolder folder = rootFolder.GetFolders(0).Cast<ITaskFolder>().FirstOrDefault(
    f => f.Name == "Windows7Course");

if (folder == null)
{
    folder = rootFolder.CreateFolder("Windows7Course", null);
}

ITaskDefinition task = scheduler.NewTask(0);
IExecAction action = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);

action.Path = typeof(SayHello.Form1).Assembly.Location;
action.WorkingDirectory = Path.GetDirectoryName(typeof(SayHello.Form1).Assembly.Location);

ISessionStateChangeTrigger trigger = (ISessionStateChangeTrigger)task.Triggers.Create(
    _TASK_TRIGGER_TYPE2.TASK_TRIGGER_SESSION_STATE_CHANGE);

trigger.StateChange = _TASK_SESSION_STATE_CHANGE_TYPE.TASK_SESSION_UNLOCK;

task.Settings.DisallowStartIfOnBatteries = true;

task.RegistrationInfo.Author = "Kate Gregory";
task.RegistrationInfo.Description = "Launches a greeting.";

IRegisteredTask ticket = folder.RegisterTaskDefinition(
    "GreetReturningUser", 
    task, 
    (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, 
    null, 
    null, 
    _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, 
    null);

This particular sample runs an exe that is in the same solution (another project). 此特定示例运行在同一解决方案(另一个项目)中的exe。 You would have to adjust the Action.Path, Action.WorkingDirectory etc to meet your needs. 您必须调整Action.Path,Action.WorkingDirectory等以满足您的需求。

Thanks for the excellent example Kate, I rue that in my wanderings I didn't come across your code first. 感谢Kate的优秀示例,我觉得在我的漫游中我没有遇到你的代码。

I've had a merry dance with registering scheduled tasks via c# code and unsurprisingly have had to tweak my schedule creation code as I encounter roadblocks on one environment that I hadn't encountered on any previously. 我通过c#代码注册计划任务有一个愉快的舞蹈,并且毫不奇怪我不得不调整我的计划创建代码,因为我在以前没有遇到的一个环境中遇到障碍。 I hope I've arrived at the most robust code and thought it might also be of benefit to others if I share some things you might try if encountering environmental issues: 我希望我已经找到了最强大的代码,并认为如果我遇到一些你遇到环境问题时可能会尝试的事情,它也可能对其他人有益:

  • Put the path to the executable in double quotes 用双引号将路径放到可执行文件中
  • Do not put the working folder in quotes 不要将工作文件夹放在引号中
  • Once the task is created, if you make subsequent changes, call the ask.RegisterChanges(); 创建任务后,如果进行后续更改,请调用ask.RegisterChanges(); - it may be worth setting the task.Definition.Principal.RunLevel = TaskRunLevel.Highest; - 可能值得设置任务.Definition.Principal.RunLevel = TaskRunLevel.Highest;
  • I typically default to a System account to run the task, but in some network environments I've encountered issues which I was unable to fathom but was able to resolve by providing a user account for the task to run under. 我通常默认使用系统帐户来运行任务,但在某些网络环境中,我遇到了一些我无法理解但能够通过提供用户帐户来解决的问题。

all the best Matt 所有最好的马特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM