.net core 定时程序
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 定时程序的更多相关文章
- C#/.NET/.NET Core定时任务调度的方法或者组件有哪些--Timer,FluentScheduler,TaskScheduler,Gofer.NET,Coravel,Quartz.NET还是Hangfire?
原文由Rector首发于 码友网 之 <C#/.NET/.NET Core应用程序编程中实现定时任务调度的方法或者组件有哪些,Timer,FluentScheduler,TaskSchedule ...
- C#/.NET/.NET Core定时任务调度的方法或者组件有哪些--Timer,FluentScheduler还是...
原文:C#/.NET/.NET Core定时任务调度的方法或者组件有哪些--Timer,FluentScheduler还是... 原文由Rector首发于 码友网 之 <C#/.NET/.NET ...
- (转) 将ASP.NET Core应用程序部署至生产环境中(CentOS7)
原文链接: http://www.cnblogs.com/ants/p/5732337.html 阅读目录 环境说明 准备你的ASP.NET Core应用程序 安装CentOS7 安装.NET Cor ...
- 在.NET Core控制台程序中使用依赖注入
之前都是在ASP.NET Core中使用依赖注入(Dependency Injection),昨天遇到一个场景需要在.NET Core控制台程序中使用依赖注入,由于对.NET Core中的依赖注入机制 ...
- 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 ...
- ASP.NET Core 中文文档 第二章 指南(8) 使用 dotnet watch 开发 ASP.NET Core 应用程序
原文:Developing ASP.NET Core applications using dotnet watch 作者:Victor Hurdugaci 翻译:谢炀(Kiler) 校对:刘怡(Al ...
- .NET跨平台之旅:探秘 dotnet run 如何运行 .NET Core 应用程序
自从用 dotnet run 成功运行第一个 "Hello world" .NET Core 应用程序后,一直有个好奇心:dotnet run 究竟是如何运行一个 .NET Cor ...
- 在Mac下运行ASP.NET Core应用程序
在Mac下运行ASP.NET Core应用程序 通过参照.NET Core相关官方文档,在我的Mac电脑上用Visual Studio Code创建了我的第一个ASP.NET应用. 开发环境搭建 首先 ...
- 将ASP.NET Core应用程序部署至生产环境中(CentOS7)
这段时间在使用Rabbit RPC重构公司的一套系统(微信相关),而最近相关检验(逻辑测试.压力测试)已经完成,接近部署至线上生产环境从而捣鼓了ASP.NET Core应用程序在CentOS上的部署方 ...
随机推荐
- 帝国CMS 7.5编辑器从WORD中粘贴过来无法保留格式和图片的解决办法
配置过滤js文件 首先打开 \editor\plugins\pastefromword\filter\default.js 在文件的最后部分又如下代码(修改前的代码),也可以搜索CKEDITO ...
- hadoop大作业
1.数据准备 2.把CSV添加到/bigdatacase/dataset中 3.检查前5行并删除第一行 4.将csv文件导入hadoop并检查前10行数据情况 5.数据文件导入hive 6.在Hive ...
- SSM项目实战 之 EasyUI
目录 EasyUI 简介 概述 使用EasyUI panel组件 简介 示例 JS形式及属性介绍 panel事件与方法 Window组件 概述 使用 行为 dialog 概述 使用 tabs组件 概述 ...
- 浅谈SOA面向服务化编程架构(dubbo)
dubbo 是阿里系的技术.并非淘宝系的技术啦,淘宝系的分布式服务治理框架式HSF啦 ,只闻其声,不能见其物.而dubbo是阿里开源的一个SOA服务治理解决方案,dubbo本身 集成了监控中心,注 ...
- HIDL概述【转】
本文转载自:https://blog.csdn.net/u013357557/article/details/84561457 HIDL背景Treble 是 Google Android 团队的一项重 ...
- 信用卡号码格式验证-C#实现
/// <summary> /// Is valid? /// </summary> /// <param name="context">Val ...
- easyui 如何为datagrid添加自定义列属性(如:width,align,editor)
我在实际业务需要为datagrid添加一个自定义属性 原先的datagrid列属性包括:title.width.align.formattter.editor等 我们可以通过datagrid的一个方法 ...
- Xamarin图表开发基础教程(9)OxyPlot框架
Xamarin图表开发基础教程(9)OxyPlot框架 OxyPlot组件构成 OxyPlot组件主要由两个类构成,分别为PlotView和PlotModel.这两个类我们在上文中也使用到了.本节将讲 ...
- Python3基础 dict __len__ 统计键值对的数量
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- [LeetCode] 88. Merge Sorted Array 合并有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...