Windows task scheduler to execute tasks in seconds

前端 未结 4 1024
北荒
北荒 2020-12-14 03:00

I\'m looking for an open source/free task scheduler for Windows 7 (development machine) that will allow me to schedule tasks (HTTP requests to a web service) to run every x

4条回答
  •  独厮守ぢ
    2020-12-14 03:41

    Short explanation: Main program starts a service process that will stay active in memory and will periodically activate a job – do something.

    1. Create a class that extends System.ServiceProcess.ServiceBase class
    2. Implement at least methods OnStart and OnStop
    3. Start and use Quartz.NET scheduler in OnStart to run tasks periodically

    Here is my template C# solution for a Windows service and a Linux demon in .NET/Mono https://github.com/mchudinov/ServiceDemon And a short blogpost about it

        class Program
        {
            public static void Main(string[] args)
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] { new ServiceDemon.Service() };
                ServiceBase.Run(ServicesToRun);
            }
        }
    
        public class Service : ServiceBase
        {
            static IScheduler Scheduler { get; set; }
    
            protected override void OnStart(string[] args)
            {
                StartScheduler();
                StartMyJob();
            }
    
            protected override void OnStop()
            {
                Scheduler.Shutdown();
            }
    
            void StartScheduler()
            {
                ISchedulerFactory schedFact = new StdSchedulerFactory();
                Scheduler = schedFact.GetScheduler();
                Scheduler.Start();
            }
    
            void StartMyJob()
            {
                var seconds = Int16.Parse(ConfigurationManager.AppSettings["MyJobSeconds"]);
                IJobDetail job = JobBuilder.Create()
                    .WithIdentity("MyJob", "group1")
                    .UsingJobData("Param1", "Hello MyJob!")
                    .Build();
    
                ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity("MyJobTrigger", "group1")
                    .StartNow()
                    .WithSimpleSchedule(x => x.WithIntervalInSeconds(seconds).RepeatForever())
                    .Build();
    
                Scheduler.ScheduleJob(job, trigger);
            }
        }
    
        public class MyJob : IJob
        {
            public void Execute(IJobExecutionContext context)
            {
                JobDataMap dataMap = context.JobDetail.JobDataMap;
                log.Info(dataMap["Param1"]);
            }
        }
    

提交回复
热议问题