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. P3396 哈希冲突(思维+方块)

    题目 P3396 哈希冲突 做法 预处理模数\([1,\sqrt{n}]\)的内存池,\(O(n\sqrt{n})\) 查询模数在范围里则直接输出,否则模拟\(O(m\sqrt{n})\) 修改则遍历 ...

  2. [C++] new和delete运算符使用方法

    new 和 delete 是C++语言中的两个运算符,配套使用. new:用于分配内存,与C语言中的 malloc 相同,分配在堆内存 delete:用于释放内存,与C语言中的 free 相同,释放堆 ...

  3. Unity3D新手入门初级教程

    根据游戏调查公司 Newzoo 针对全球手机游戏市场所做的调查报告显示,2016年全球游戏市场规模将达到 996 亿美元,其中手机游戏市场将以 21.3% 的增幅获得约 369 亿美元的收入,而中国手 ...

  4. JdkDynamicAopProxy与CglibAopProxy介绍

    继续上一篇的介绍 1.上一篇分析到createAopProxy方法,创建Aop代理对象 protected final synchronized AopProxy createAopProxy() { ...

  5. openstack错误问题定位及调试

  6. libusb: android上集成libusb库

    1. 下载libusb库. 可以到libusb库的官网(https://libusb.info/)或者是其官方的github仓库(https://github.com/libusb/libusb/re ...

  7. docker 部署jar包

    docker volume create j1ls /var/lib/docker/volumes/j1/_data 上传jardocker run -it -d --name j1 -v j1:/h ...

  8. 【转载】 《Human-level concept learning through probabilistic program induction》阅读笔记

    原文地址: https://blog.csdn.net/ln1996/article/details/78459060 --------------------- 作者:lnn_csdn 来源:CSD ...

  9. Hadoop,Spark,Flink 相关KB

    Hive: https://stackoverflow.com/questions/17038414/difference-between-hive-internal-tables-and-exter ...

  10. Redis 密码设置 及 带密码访问

    转: Redis 密码设置 如果不加密码,默认只能本机访问,加密码也是为了安全考虑 1.进入Redis 的安装目录,找到redis.conf文件.用vi命令打开文件 输入  / requirepass ...