using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Quartz; using Quartz.Impl; using System; using System.Collections.Specialized; using System.IO; using System.Text; using System.Threading.Tasks; namespace Star.Service.News { class Program { static void Main(string[] args) { Console.WriteLine("Time Job Start"); RunProgram().GetAwaiter().GetResult(); Console.WriteLine("Hello World!"); Console.Read(); } private static async Task RunProgram() { try { // Grab the Scheduler instance from the Factory NameValueCollection props = new NameValueCollection { { "quartz.serializer.type", "binary" } }; Factory the StdSchedulerFactory = new new the StdSchedulerFactory (The props); IScheduler Scheduler = the await factory.GetScheduler (); // start the task scheduler the await scheduler.Start (); // define a custom timing Task Job class IJobDetail job = JobBuilder.Create <TimedBackgroundService > () .WithIdentity ( " the jobs that job1 " , " named group1 " ) .build (); ISimpleTrigger Trigger = (ISimpleTrigger) TriggerBuilder.Create () .WithIdentity ( " trigger1 " ) // to task a name .StartAt (DateTime.Now) // set the task start time .ForJob ( " the jobs that job1 " , " group1 " ) // to specify a task group .WithSimpleSchedule (x => X .WithIntervalInSeconds ( 180 [ ) // cycle time of 1 second time .RepeatForever ()) .build (); // wait for the mission the await scheduler.ScheduleJob (job, Trigger); // some sleep to show what's happening //await Task.Delay(TimeSpan.FromMilliseconds(2000)); } catch (SchedulerException se) { await Console.Error.WriteLineAsync(se.ToString()); } } } }
using Quartz; using Star.Helpers; using System; using System.Diagnostics; using System.Threading.Tasks; namespace Star.Service.News { public class TimedBackgroundService : IJob { public Task Execute(IJobExecutionContext context) { //return Console.Out.WriteLineAsync("Greetings from HelloJob!"); JobKey key = context.JobDetail.Key; Process Proc = null; string exeName = "main"; try { string destFilePath = "c://chromedriver.exe"; string sourceFilePath = CommonHelper.GetPhysicalPath($"newspicker\\chromedriver.exe"); if (!FileHelper.IsExistFile(destFilePath)) { FileHelper.CopyTo(sourceFilePath, destFilePath); } var myProcess = Process.GetProcesses(); foreach (Process MyProcess in myProcess) { //查找是否正在运行 if (MyProcess.ProcessName.CompareTo(exeName) == 0 || MyProcess.ProcessName.CompareTo("chrome") == 0 || MyProcess.ProcessName.CompareTo("chromedriver") == 0) { MyProcess.Kill(); //杀死当前程序 } } } catch (Exception) { } try { string fileurl = CommonHelper.GetPhysicalPath($".exe exeName} { " ); // Start external program Proc = the Process.Start (fileurl); Proc.WaitForExit ( 120 * 1000 ); Proc.CloseMainWindow (); // send close to the message by the process to close the main window has a user interface process context.MergedJobDataMap.Put ( " RunResult " , ApiResult.Success ($ " news gathering program run time {(Proc.StartTime - Proc.ExitTime) .Milliseconds} " )); } the catch (Exception EX) { proc ? .WaitForExit ( 60 *1000 ); Proc ? .CloseMainWindow (); Proc ? .Close (); context.MergedJobDataMap.Put ( " RunResult " , ApiResult.Fail (ApiEnum.Error, $ " {{key.Name key.Group}}: {. } ex.Message " )); } the finally { Proc.Close (); // release all the components associated with this resource } return Task.CompletedTask; } } }