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() //循环的时间 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) ==
|| MyProcess.ProcessName.CompareTo("chrome") ==
|| MyProcess.ProcessName.CompareTo("chromedriver") == )
{
MyProcess.Kill(); //杀死当前程序
}
}
}
catch (Exception)
{ } try
{
string fileurl = CommonHelper.GetPhysicalPath($"{exeName}.exe");
//启动外部程序
Proc = Process.Start(fileurl); Proc.WaitForExit( * );
Proc.CloseMainWindow();//通过向进程的主窗口发送关闭消息来关闭拥有用户界面的进程 context.MergedJobDataMap.Put("RunResult", ApiResult.Success($"新闻采集程序运行时间{(Proc.StartTime - Proc.ExitTime).Milliseconds}"));
}
catch (Exception ex)
{
Proc?.WaitForExit( * );
Proc?.CloseMainWindow();
Proc?.Close(); context.MergedJobDataMap.Put("RunResult", ApiResult.Fail(ApiEnum.Error, $"{key.Group}.{key.Name}:{ ex.Message}"));
}
finally
{
Proc.Close();//释放与此组件关联的所有资源
}
return Task.CompletedTask;
}
}
}

.net core 定时程序的更多相关文章

  1. C#/.NET/.NET Core定时任务调度的方法或者组件有哪些--Timer,FluentScheduler,TaskScheduler,Gofer.NET,Coravel,Quartz.NET还是Hangfire?

    原文由Rector首发于 码友网 之 <C#/.NET/.NET Core应用程序编程中实现定时任务调度的方法或者组件有哪些,Timer,FluentScheduler,TaskSchedule ...

  2. C#/.NET/.NET Core定时任务调度的方法或者组件有哪些--Timer,FluentScheduler还是...

    原文:C#/.NET/.NET Core定时任务调度的方法或者组件有哪些--Timer,FluentScheduler还是... 原文由Rector首发于 码友网 之 <C#/.NET/.NET ...

  3. (转) 将ASP.NET Core应用程序部署至生产环境中(CentOS7)

    原文链接: http://www.cnblogs.com/ants/p/5732337.html 阅读目录 环境说明 准备你的ASP.NET Core应用程序 安装CentOS7 安装.NET Cor ...

  4. 在.NET Core控制台程序中使用依赖注入

    之前都是在ASP.NET Core中使用依赖注入(Dependency Injection),昨天遇到一个场景需要在.NET Core控制台程序中使用依赖注入,由于对.NET Core中的依赖注入机制 ...

  5. ASP.NET Core 中文文档 第二章 指南(1)用 Visual Studio Code 在 macOS 上创建首个 ASP.NET Core 应用程序

    原文:Your First ASP.NET Core Application on a Mac Using Visual Studio Code 作者:Daniel Roth.Steve Smith ...

  6. ASP.NET Core 中文文档 第二章 指南(8) 使用 dotnet watch 开发 ASP.NET Core 应用程序

    原文:Developing ASP.NET Core applications using dotnet watch 作者:Victor Hurdugaci 翻译:谢炀(Kiler) 校对:刘怡(Al ...

  7. .NET跨平台之旅:探秘 dotnet run 如何运行 .NET Core 应用程序

    自从用 dotnet run 成功运行第一个 "Hello world" .NET Core 应用程序后,一直有个好奇心:dotnet run 究竟是如何运行一个 .NET Cor ...

  8. 在Mac下运行ASP.NET Core应用程序

    在Mac下运行ASP.NET Core应用程序 通过参照.NET Core相关官方文档,在我的Mac电脑上用Visual Studio Code创建了我的第一个ASP.NET应用. 开发环境搭建 首先 ...

  9. 将ASP.NET Core应用程序部署至生产环境中(CentOS7)

    这段时间在使用Rabbit RPC重构公司的一套系统(微信相关),而最近相关检验(逻辑测试.压力测试)已经完成,接近部署至线上生产环境从而捣鼓了ASP.NET Core应用程序在CentOS上的部署方 ...

随机推荐

  1. HTML列表,表格与媒体元素

    一.列表  信息资源的一种展示形式   二.列表的分类  1.有序列表   <ol>    <li>列表项1</li>    <li>列表项2</ ...

  2. Windows 安装R

    下载 R 的安装包 双击 安装包 进行安装 安装完成 测试 修改 R 中的CRAN镜像 添加到 Windows 的环境变量中 测试

  3. 东软HIS系统_打印发票提示“打印机报错!对访问XXX的设置无效”解决办法

    发票打印报错 添加打印机,端口跟物理打印机同一个. 添加 MZJSFP,ZYJSFP,YJJFP三个打印机 打印发票错位 设置自定义纸张 MZJSFP 宽30.40CM 高12.94CM ZYJSFP ...

  4. python selenium chrome 实现自动化登录

    1.环境安装 selenium的开发文档网址(英语好的可以直接看这个,写的很详细):http://selenium-python.readthedocs.io/ 因为实现的时候使用的是谷歌浏览器,在运 ...

  5. 自定义设置jqGrid的标头居中加粗等

    beforeRequest: function () { $("thead th").css("text-align", "center") ...

  6. md5签名加密(用于java 后台调用短信平台接口实现发短信)

    MD5Util 方法 package com.funcanteen.business.action.pay.util; import java.security.MessageDigest; publ ...

  7. 《你必须知道的javascript(上)》- 1.作用域和闭包

    1 作用域是什么 1.1 编译原理 分词/词法分析(Tokenizing/Lexing) 将由字符组成的字符串分解成(对编程语言来说)有意义的代码块,这些代码块被称为词法单元(token). 解析/语 ...

  8. Android adb临时关闭Selinux

    在eng/userdebug版本中 使用getenforce 命令查询当前权限状态,如:adb shell getenforce 使用setenforce 命令进行设置:adb shell seten ...

  9. Program不是内部命令也不是外部命令

    在项目中使用java动态生成.bat文件,再调试时执行bat文件失败! 显示:Program不是内部命令也不是外部命令 百度了一下:C:\"Program Files"或progr ...

  10. Swift编码总结5

    1.UIWindow属性: 1>.- (void)becomeKeyWindow;                               // override point for sub ...