1

I have a database which have columns for process name and time. I have a windows service which have a list of process name and time fetched from DB and now want to trigger that process on that specific time. Is there a way that I can trigger these processes(methods) for list of scheduled time by itself?

This is what I tried so far..I want to run report method to get trigger at different time events as my schedule list e.g. 10:00 AM, 12:20 pm, 1:45 pm...etc once my windows service is running.

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();
            }
        }
    }
rajcool111
  • 657
  • 4
  • 14
  • 36

2 Answers2

0

If they are executable files, or batch files, you can use Process.Start. ShellExecute equivalent in .NET

Community
  • 1
  • 1
tgolisch
  • 6,549
  • 3
  • 24
  • 42
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/12323804) – Mostafiz May 12 '16 at 04:52
  • Normally, I would agree with you and change this answer. However, the question didn't give enough information to make a useable example for Process.Start(). The link simply points to another SO article. – tgolisch May 12 '16 at 12:13
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();
    }
}
Esteban Elverdin
  • 3,552
  • 1
  • 17
  • 21
  • That is close! What if I have a list for hour and minutes. And I want to schedule it for multiple hour/time? – rajcool111 Sep 24 '13 at 19:08
  • You can create many triggers and add them to the scheduler. Checking your last update, you can put the create and add trigger to the quartz schedeler inside the foreach you have. Then in YourProcess class just RunReport() method. – Esteban Elverdin Sep 24 '13 at 19:17
  • I tried that after it runs the job its giving error "jobrunshell.cs not found" – rajcool111 Sep 24 '13 at 20:54
  • Check the updated answer, I tested that code and it works. Maybe you are having something wrong. Let me know. – Esteban Elverdin Sep 24 '13 at 22:17