简体   繁体   English

安装程序中的Vista计划任务

[英]Vista Schedule Task From Setup

I'm deploying a C# application using the Setup Wizard project in Visual Studio 2008. 我正在使用Visual Studio 2008中的安装向导项目部署C#应用程序。

What is the simplest way for me to have Windows schedule my application to run at regular intervals (eg every 8 hours)? 对于我来说,让Windows安排我的应用程序定期运行(例如每8小时),最简单的方法是什么? I prefer if this scheduling would happen during the application's installation to simplify setup for the end-user. 我更喜欢在应用程序安装期间进行此调度以简化最终用户的设置。

Thanks! 谢谢!

This took some putting together for me, so here's complete documentation for scheduling a task from a setup project. 这需要为我整理一些,所以这里有完整的文档,用于从安装项目安排任务。

Once you have your deployment project created, you're going to need to use Custom Actions to schedule the task. 创建部署项目后,您将需要使用自定义操作来安排任务。 Walkthrough: Creating a Custom Action 演练:创建自定义操作

Note: The walkthrough asks you to add the primary output to the Install node, even if you don't plan on doing anything custom during the Install step. 注意:演练要求您将主输出添加到“安装”节点,即使您不打算在“安装”步骤期间执行任何自定义操作也是如此。 This is important, so don't ignore it like I did. 这很重要,所以不要像我一样忽略它。 The Installer Class does some state management during this step, and needs to run. 安装程序类在此步骤中执行一些状态管理,并且需要运行。

The next step is to pass the installation directory to the custom action. 下一步是将安装目录传递给自定义操作。 This is done via the CustomActionData property . 这是通过CustomActionData属性完成的。 I entered /DIR="[TARGETDIR]\\" for the commit node (I schedule my task during the commit step). 我为提交节点输入了/DIR="[TARGETDIR]\\" (我在提交步骤中安排了我的任务)。 MSDN: CustomActionData Property MSDN:CustomActionData属性

Finally, you'll need to either access the task scheduling API, or use Process.Start to call schtasks.exe . 最后,您需要访问任务调度API,或使用Process.Start调用schtasks.exe The API will give you a more seamless and robust experience, but I went with the schtasks route because I had the command line handy. API将为您提供更加无缝和强大的体验,但我使用了schtasks路由,因为我有方便的命令行。

Here is the code I ultimately ended up with. 这是我最终得到的代码。 I registered it as a custom action for install, commit, and uninstall. 我将其注册为安装,提交和卸载的自定义操作。

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Security.Permissions;
using System.Diagnostics;
using System.IO;


namespace MyApp
{
    [RunInstaller(true)]
    public partial class ScheduleTask : System.Configuration.Install.Installer
    {
        public ScheduleTask()
        {
            InitializeComponent();
        }

        [SecurityPermission(SecurityAction.Demand)]
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);

            RemoveScheduledTask();

            string installationPath = Context.Parameters["DIR"] ?? "";
            //Without the replace, results in c:\path\\MyApp.exe
            string executablePath = Path.Combine(installationPath, "MyApp.exe").Replace("\\\\", "\\");

            Process scheduler = Process.Start("schtasks.exe",string.Format("/Create /RU SYSTEM /SC HOURLY /MO 2 /TN \"MyApp\" /TR \"\\\"{0}\\\"\" /st 00:00", executablePath));
            scheduler.WaitForExit();
        }

        [SecurityPermission(SecurityAction.Demand)]
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
            RemoveScheduledTask();
        }

        private void RemoveScheduledTask() {
            Process scheduler = Process.Start("schtasks.exe", "/Delete /TN \"MyApp\" /F");
            scheduler.WaitForExit();
        }
    }
}

Scheduled task is your way to go. 计划任务是您的最佳选择。 Take a look at this page for how to setup the task with a script . 请查看此页面,了解如何使用脚本设置任务。

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

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