Windows Service 服务搭配FluentScheduler实现定时任务调度
Windows Service 服务
创建Windows Service 项目
- 创建一个
Windows Service
项目,并将项目名称改为TaskWindowService
- 在解决方案资源管理器内将
Service1.cs
改为TaskService.cs
- 在服务启动和结束时,记录日志
protected override void OnStart(string[] args)
{
LogHelper.Log("服务启动!");
}
protected override void OnStop()
{
LogHelper.Log("服务停止!");
}
- 打开
TaskService
的设计界面,右键“添加安装程序” - 此时软件会生成两个组件,分别为
serviceInstaller1
及serviceProcessInstaller1
- 点击
serviceInstaller1
,在“属性”窗体将ServiceName
改为TaskService
,Description
改为我的服务,StartType
保持为Manual
- 点击
serviceProcessInstaller1
,在“属性”窗体将Account
改为LocalSystem
(服务属性系统级别)
- 鼠标右键点击项目
TaskWindowService
,在弹出的上下文菜单中选择“生成”按钮,到此WindowService
项目搭建完毕
创建安装、启动、停止、卸载服务的Windows窗体
- 新建一个
Windows Form
项目,并命名为WindowsServiceClient
- 添加引用将
TaskWindowService
添加到WindowsServiceClient
中,并将其设置为“启动项目” - 创建安装、启动、停止、卸载 按钮并创建点击事件
string serviceFilePath = $"{Application.StartupPath}\\TaskWindowsService.exe";
string serviceName = "TaskService";
//事件:安装服务
private void button1_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName);
this.InstallService(serviceFilePath);
}
//事件:启动服务
private void button2_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
}
//事件:停止服务
private void button4_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
}
//事件:卸载服务
private void button3_Click(object sender, EventArgs e)
{
if (this.IsServiceExisted(serviceName))
{
this.ServiceStop(serviceName);
this.UninstallService(serviceFilePath);
}
}
//判断服务是否存在
private bool IsServiceExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController sc in services)
{
if (sc.ServiceName.ToLower() == serviceName.ToLower())
{
return true;
}
}
return false;
}
//安装服务
private void InstallService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
IDictionary savedState = new Hashtable();
installer.Install(savedState);
installer.Commit(savedState);
MessageBox.Show("服务安装成功!");
}
}
//卸载服务
private void UninstallService(string serviceFilePath)
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
installer.Uninstall(null);
MessageBox.Show("服务卸载成功!");
}
}
//启动服务
private void ServiceStart(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Stopped)
{
control.Start();
MessageBox.Show("服务启动成功!");
}
}
}
//停止服务
private void ServiceStop(string serviceName)
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Running)
{
control.Stop();
MessageBox.Show("服务停止成功!");
}
}
}
直接运行WindowsServiceClient
项目,并点击“安装服务”会报错提示权限不足。
此时可以打开项目的Debug
文件夹,找到WindowsServiceClient.exe
文件,右键以管理员身份运行。
使用WIN+R
的方式打开运行窗体,并在窗体内输入services.msc
后打开服务,可以找到刚刚安装的TaskService
。
若需要直接运行项目来打开窗体安装服务,则需要使用UAC中Administrator的权限。在WindowsServiceClient
项目中添加“应用程序清单文件”。
打开该文件将asInvoker
修改为requireAdministrator
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
重启项目后就可以直接在VS中安装服务了。
以上内容主要转载自 https://www.cnblogs.com/cncc/p/7170951.html 然后做了一些小改动。
FluentScheduler 定时任务
关于FluentScheduler
详细的介绍可以看下github的说明文档,介绍了所有的调用方式 https://github.com/fluentscheduler/FluentScheduler
- 在
TaskWindowsService
项目中通过nuget
添加FluentScheduler
包
- 在
TaskService
中,服务启动的地方注册定时任务
protected override void OnStart(string[] args)
{
LogHelper.Log("服务启动!");
//每10秒执行一次任务
var registry = new Registry();
registry.Schedule(() =>
{
LogHelper.Log("服务运行中,执行定时任务!");
}).ToRunEvery(10).Seconds();
JobManager.Initialize(registry);
}
重新生成项目->运行->安装服务->运行服务,打开日志文件可以看到定时重新的任务的输出记录。
Windows Service 服务搭配FluentScheduler实现定时任务调度的更多相关文章
- quartz.net结合Topshelf实现windows service服务托管的作业调度框架
topshelf可以很简单方便的实现windows service服务,详见我的一篇博客的介绍 http://www.cnblogs.com/xiaopotian/articles/5428361.h ...
- C# Windows Service服务的创建和调试
前言 关于Windows服务创建和调试的文章在网络上的很多文章里面都有,直接拿过来贴在这里也不过仅仅是个记录,不会让人加深印象.所以本着能够更深刻了解服务项目的创建和调试过程及方法的目的,有了这篇记录 ...
- .Net Windows Service(服务) 调试安装及System.Timers.Timer 使用
Windows Service(服务) 是运行在后台的进程 1.VS建立 Windows 服务(.NET Framework) 2.添加Timer 双击Service1.cs可以拖控件(System ...
- 震惊!Windows Service服务和定时任务框架quartz之间原来是这种关系……
过场CG: 接到公司领导的文件指示,“小熊”需要在6月底去海外执行一个行动代号为[定时任务]的营救计划,这个计划关系到公司某个项目的生死(数据安全漏洞),作战部拟定两个作战方案: 方案一:使用务定 ...
- C#制作Windows service服务系列二:演示一个定期执行的windows服务及调试(windows service)
系列一: 制作一个可安装.可启动.可停止.可卸载的Windows service(downmoon原创) 系列二:演示一个定期执行的windows服务及调试(windows service)(down ...
- [开发笔记]-Windows Service服务相关注意事项
注意一:报错:“本地计算机上的 *** 服务启动后停止.某些服务在未由其他服务或程序使用时将自动停止.” 该问题主要的原因是 Service服务程序中有错误. 遇到这个问题时,无论是重新安装服务,还是 ...
- war包部署在tomcat下,使用windows service服务方式启动tomcat服务器,在包含调用dll的模块,报dll找不到问题的解决办法
问题描述: 开发了一个需要调用dll的java web程序,在idea开发环境下运行调试没问题,可以正常运行,在tomcat/bin下,运行批处理startup.bat,启动tomcat服务器,也可以 ...
- [开发笔记]-控制Windows Service服务运行
用代码实现动态控制Service服务运行状态. 效果图: 代码: #region 启动服务 /// <summary> /// 启动服务 /// </summary> /// ...
- 使用C#编程语言开发Windows Service服务
转载-https://www.cnblogs.com/yubao/p/8443455.html Create Windows Service project using Visual Studio C ...
随机推荐
- 只想听歌曲的高潮部分?让我用python来教你做个音乐高潮提取器!
有些时候,我们为了设定手机铃声或者发抖音视频时,会耗费大量时间在音乐剪辑上.尤其是想发布大量抖音视频的时候,我们得收集大量的短音乐,这是一个相当耗费时间的工作.那么,这个音乐高潮的提取能不能自动化呢? ...
- 易优CMS:【小白学标签】之empty的基础用法
[基础用法] 名称:empty 功能:判断某个变量是否为空,可以嵌套到任何标签里面使用,比如:channel.type等 语法: {eyou:empty name='$eyou.field.seo_t ...
- linux shell通过curl获取HTTP请求的状态码
直接上代码: curl -I -m -o /dev/null -s -w %{http_code} www.baidu.com 参数说明: -I 仅测试HTTP头 -m 10 最多查询10s -o / ...
- 用linux编译并运行c文件
目录 创建一个.c文件 写完代码以后进行编译 @(用linux编译并运行c文件) 创建一个.c文件 vi 文件名.c 对于图形化的linux,需要右键桌面,在终端中打开,输入vi 文件名.c就创建了一 ...
- Python中线程的使用
并发:多个任务同一时间段进行 并行:多个任务同一时刻进行 线程的实现 线程模块 Python通过两个标准库_thread 和threading,提供对线程的支持 , threading对_thread ...
- python 将带有TZ的UTC时间字符串,转换成本地时间
整个地球分为二十四时区,每个时区都有自己的本地时间.在国际无线电通信中,为统一而普遍使用一个标准时间,称为通用协调时(UTC, Universal Time Coordinated).UTC与格林尼治 ...
- 2.Java基础_Java常量
/* 常量: 在程序执行过程中,其值不可以发生改变的量 常量分类: 字符串常量: 用双引号括起来的内容. "Hello,World!" 整数常量: 不带小数的数字. 666,-88 ...
- unittest执行顺序,使用unittest.main()按照test开头,由0-9,A-Z,a-z的顺序执行; 可使用TestSuite类的addTest方法改变执行顺序;
import unittestclass Study(unittest.TestCase): # def setUp(self): # print('start') # def tearDown(se ...
- html学习之二(常用标签练习)
<!DOCTYPE html><head> <meta charset="utf-8"> <title>锚点链接</title ...
- 【声明式事务】Spring声明式事务实现(三)
以MyBatis为例. 一.基于注解的声明式事务配置 1. 添加tx名字空间 xmlns:tx="http://www.springframework.org/schema/tx" ...