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

发布于 2024-09-12 06:26:12 字数 213 浏览 3 评论 0原文

我正在开发基于附加的应用程序。每个附加组件都使用调度程序。已加载附加计划任务。我的应用程序只运行一个实例。有时应用程序关闭,有时正在运行。因此我需要使用 Windows 7 任务计划程序

如何在自己的应用程序上使用任务计划程序?

我需要从应用程序创建新任务
我需要在任务完成后,向我的应用程序发送消息或调用我的应用程序的任何功能
可能吗?
怎么做?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫轩蝶泪 2024-09-19 06:26:12

在 CodeProject 上查看这个项目。

.NET 的新任务计划程序类库

Check out this project at CodeProject.

A New Task Scheduler Class Library for .NET

她说她爱他 2024-09-19 06:26:12

如果您想通过代码与 Windows 7 计划任务系统交互来创建、管理或删除任务,那是没有问题的。 (我在为 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);

此特定示例运行同一解决方案(另一个项目)中的 exe。您必须调整 Action.Path、Action.WorkingDirectory 等以满足您的需求。

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. (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:

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). You would have to adjust the Action.Path, Action.WorkingDirectory etc to meet your needs.

别忘他 2024-09-19 06:26:12

感谢凯特提供的出色示例,我后悔在我的漫游中没有首先遇到您的代码。

我通过 C# 代码注册计划任务进行了一段快乐的舞蹈,毫不奇怪的是,当我在一个环境中遇到我以前从未遇到过的障碍时,我不得不调整我的计划创建代码。我希望我已经得到了最强大的代码,并且认为如果我分享一些您在遇到环境问题时可能会尝试的事情,它也可能对其他人有益:

  • 将可执行文件的路径放在双引号中
  • 不要将工作文件夹放在任务
  • 创建后,如果进行后续更改,请调用ask.RegisterChanges(); - 可能值得设置task.Definition.Principal.RunLevel = TaskRunLevel.Highest;
  • 我通常默认使用系统帐户来运行任务,但在某些网络环境中,我遇到了无法理解的问题,但可以通过提供运行任务的用户帐户来解决。

一切顺利
马特

Thanks for the excellent example Kate, I rue that in my wanderings I didn't come across your code first.

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. 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(); - it may be worth setting the task.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文