-1

我创建了一个在后台特定时间执行 exe 的服务。我想设置一个计时器,使服务每天同时执行 exe.. 怎么能做到..??? 这是我在服务类中编写的代码:

public partial class Service1 : ServiceBase
{
    string time = DateTime.Now.ToShortTimeString();
    Process p = new Process();

    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += new DoWorkEventHandler(DoWork);
        bw.RunWorkerAsync();
    }

    private void DoWork(object sender, DoWorkEventArgs e)
    {
        p.StartInfo = new ProcessStartInfo(@"c:\windows\ccm\ccmeval.exe");

        if (time == "6:55 PM")
        {
            p.Start();
            p.WaitForExit();
            base.Stop();
        }
    }

    protected override void OnStop()  { }
4

1 回答 1

0

假设您想在每天早上 6:30 运行每个任务

        var dtTimeToStartAt = DateTime.Today.AddHours(6).AddMinutes(30);
        dtTimeToStartAt = DateTime.Now > dtTimeToStartAt ? dtTimeToStartAt.AddDays(1) : dtTimeToStartAt;

        var timer = new System.Threading.Timer(x => YourCallbackMethod(), new object() /*not much use*/,
                                           dtTimeToStartAt.Subtract(DateTime.Now), new TimeSpan(24, 0, 0));
于 2013-07-23T06:04:22.277 回答