1

我有一个数据库,其中包含进程名称和时间列。我有一个 Windows 服务,它有一个从数据库中获取的进程名称和时间列表,现在想在那个特定时间触发该进程。有没有一种方法可以让我自己触发这些进程(方法)以获取预定时间列表?

这是我迄今为止尝试过的......我想运行报告方法以在不同的时间事件触发作为我的日程安排列表,例如上午 10:00、下午 12:20、下午 1:45 ......一旦我的 Windows 服务是跑步。

public override void Run()
    {
    var _dateNow = DateTime.Now();
        _schedule = scheduleProvider.GetSchedules();

        foreach (ProcessEngineSchedule sc in _schedule)
        {
            if (_dateNow.Hour == sc.Time.Hour && _dateNow.Minute == sc.Time.Minute)
            {
                RunReport();
            }
        }
    }
4

2 回答 2

0

Add latest version of Quartz.net from Nuget to your project

Then you can use a code similar to this for starting the process each day at specified hour and minutes. You can play with the trigger options, there are many alternatives.

EDIT: Updated to reflect code posted in question update

    _schedule = scheduleProvider.GetSchedules();

    ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

    scheduler = schedulerFactory.GetScheduler();

    var count = 0;
    foreach (ProcessEngineSchedule sc in _schedule)
    {
        IJobDetail job = JobBuilder.Create<YourProcess>()
              .WithIdentity("YourProcess" + count.ToString(), null)
              .Build();

        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("YourProcess" + count.ToString(), null)
            .WithDailyTimeIntervalSchedule(x => x
                                 .OnEveryDay()
                                   .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(sc.Time.Hour, sc.Time.Minute)))
                    .Build();

        scheduler.ScheduleJob(job, trigger);

        count++;
    }

    scheduler.Start();

Then your process should look like this

public class YourProcess: IJob
{

    public void Execute(IJobExecutionContext context)
    {
       //Your code goes here
        RunReport();
    }
}
于 2013-09-24T15:06:54.703 回答
0

如果它们是可执行文件或批处理文件,您可以使用 Process.Start。 .NET 中的 ShellExecute 等效项

于 2013-09-24T14:27:02.703 回答