前言:实现定时任务的方案有很多,服务器是Windows系统的用Windows的service实现定时任务的调用还是很稳定的,之前我们项目是直接在后台增加定时器,这种解决方案 每当后台部署或者iis重启它都会重新计时所以非常不靠谱而且还可能造成线程堵塞。
一,VS创建Windows服务 项目。
二,添加安装程序
修改安装配置
Description(系统服务的描述)
DisplayName (系统服务中显示的名称)
ServiceName(系统事件查看器里的应用程序事件中来源名称)
serviceProcessInstaller1属性设置:Account 下拉设置成 LocalSystem
设置服务启动方式为自动启动,ProjectInstaller.cs代码设置
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Threading.Tasks;
namespace TestService
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
this.Committed += new InstallEventHandler(ProjectInstaller_Committed);
}
private void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{
//参数为服务的名字
System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("TestService");
controller.Start();
}
private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
}
private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
}
}
}
三,打开项目编写启动类
在OnStart中写入两个定时器,让service启动时触发它。
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
//写入自己的日志,方便查看启动情况
Common.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Service Start.");
Timer timer = new Timer();//new一个定时器
timer.Interval = 10 * 1000;//10秒
timer.AutoReset = true;//是否重复执行方法
timer.Enabled = true;//是否执行方法
timer.Elapsed += new ElapsedEventHandler(Timing10S);//到时间调用方法
Timer t = new Timer();
t.Interval = 6 * 60 * 60 * 1000;//6个小时
t.AutoReset = true;
t.Enabled = true;
t.Elapsed += new ElapsedEventHandler(Timing6H);//到时间调用方法
}
private void Timing10S(object source, System.Timers.ElapsedEventArgs e)
{
string res = string.Empty;
try
{
//每10秒钟触发的任务,可以调用后台接口等
//....
res = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "--Timing10S--SUCCESS";
}
catch (Exception exc)
{
res = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "--Timing10S--" + exc.Message;
}
finally
{
//写入自己的日志,方便查看错误
Common.WriteLog(res);
}
}
private void Timing6H(object source, System.Timers.ElapsedEventArgs e)
{
string res = string.Empty;
try
{
//每6个小时触发的任务
//....
res = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "--Timing6H--SUCCESS";
}
catch (Exception exc)
{
res = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "--Timing6H--" + exc.Message;
}
finally
{
//写入自己的日志,方便查看错误
Common.WriteLog(res);
}
}
protected override void OnStop()
{
//写入自己的日志,查看关闭情况
Common.WriteLog("Stop...");
}
}
四,启动关闭的方法
管理员运行cmd
进入vs坏境
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
安装service
installUtil "绝对路径\Test_Service.exe"
卸载service
installUtil /u "绝对路径\Test_Service.exe"
打开任务管理器 --》服务 查看PID
查看PID运行状态
netstat -ano|findstr 13536
强制关闭13536
taskkill /f /pid 13536
具体操作
更新服务的exe文件 只要卸载service就可以进行文件的替换,然后安装service就好了。
如果service卸载报错 就只能先强行关闭 然后再卸载。
备注:报错可以先查看一下电脑的Windows service中有没有这个任务,如果有手动运行查看Windows返回结果,然后再改代码。
-----------------------END------------------------
有问题欢迎留言