using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Quartz; using Quartz.Imp... ...
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" } }; StdSchedulerFactory factory = new StdSchedulerFactory(props); IScheduler scheduler = await factory.GetScheduler(); // 啟動任務調度器 await scheduler.Start(); // 定義一個 Job 自定義定時任務類 IJobDetail job = JobBuilder.Create<TimedBackgroundService>() .WithIdentity("job1", "group1") .Build(); ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create() .WithIdentity("trigger1") // 給任務一個名字 .StartAt(DateTime.Now) // 設置任務開始時間 .ForJob("job1", "group1") //給任務指定一個分組 .WithSimpleSchedule(x => x .WithIntervalInSeconds(180) //迴圈的時間 1秒1次 .RepeatForever()) .Build(); // 等待執行任務 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($"{exeName}.exe"); //啟動外部程式 Proc = Process.Start(fileurl); Proc.WaitForExit(120 * 1000); Proc.CloseMainWindow();//通過向進程的主視窗發送關閉消息來關閉擁有用戶界面的進程 context.MergedJobDataMap.Put("RunResult", ApiResult.Success($"新聞採集程式運行時間{(Proc.StartTime - Proc.ExitTime).Milliseconds}")); } catch (Exception ex) { Proc?.WaitForExit(60 * 1000); Proc?.CloseMainWindow(); Proc?.Close(); context.MergedJobDataMap.Put("RunResult", ApiResult.Fail(ApiEnum.Error, $"{key.Group}.{key.Name}:{ ex.Message}")); } finally { Proc.Close();//釋放與此組件關聯的所有資源 } return Task.CompletedTask; } } }