.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上的部署方 ...
随机推荐
- windows 共享网络
windows 共享网络 今天单位的网络突然断了,光猫LOS亮红灯,宽带报修.等了半天还没来,下面科室要上报资料,急着用网, 通过windows的共享网络+无线网卡暂时用我的手机流量. 找了一台电脑插 ...
- rust所有权
所有权与函数 fn main() { let s = String::from("hello"); takes_ownership(s); //s的值移动到函数里 let x = ...
- java.lang.Thread类的静态方法sleep()和yield()的比较
[线程让步yield()方法] yield()方法可以让当前正在执行的线程暂停,但它不会阻塞该线程,它只是将该线程从运行状态转入就绪状态. 只是让当前的线程暂停一下,让系统的线程调度器重新调度一次. ...
- Mac使用秘钥登录Linux服务器
简介 在 Mac 上配置 SSH 密钥登录远程的 Linux 相关配置 1.创建本地的 SSH 密钥 本地 生成秘钥对 ssh-keygen -t rsa -C 'youxiang@aliyun.co ...
- tar加密码
tar -zcvf - *** | openssl des3 -salt -k pass | dd of=.his dd if=.his | openssl des3 -d -k pass| tar ...
- Real-time ‘Actor-Critic’ Tracking
Real-time ‘Actor-Critic’ Tracking 2019-07-15 10:49:16 Paper: http://openaccess.thecvf.com/content_EC ...
- ThinkPHP5 基础知识入门 [入门必先了解]
一.目录结构 下载最新版框架后,解压缩到web目录下面,可以看到初始的目录结构如下: project 应用部署目录 ├─application 应用目录(可设置) │ ├─common 公共模块目录( ...
- Python装饰器之functools.wraps的作用
# -*- coding: utf-8 -*- # author:baoshan def wrapper(func): def inner_function(): pass return inner_ ...
- springboot使用SpringTask实现定时任务
SpringTask是Spring自主研发的轻量级定时任务工具,相比于Quartz更加简单方便,且不需要引入其他依赖即可使用. 只需要在配置类中添加一个@EnableScheduling注解即可开启S ...
- 【Python学习之六】文件操作
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 python3.6 一.文件的打开与关闭 使用open函数,可以打开一 ...