C#使用定时任务框架Windows.TaskSchedule.exe安装控制台应用程序创建的Windows服务

上一篇介绍了利用模板创建、安装以及卸载Windows服务,本篇继续研究这个话题,不过是创建控制台程序,使用Windows.TaskSchedule.exe安装

为什么要使用控制台程序?——调试简单粗暴。哈哈。

创建控制台应用程序项目后,我们编辑服务主体代码,我们继续向文本文件中写入一句话。

在实现具体逻辑前,我们需要引用Windows.TaskSchedule.Extends.dll,服务类继承 IJob

 1 using System;
 2 using System.Configuration;
 3 using System.IO;
 4 using Windows.TaskSchedule.Extends;
 5 
 6 namespace MyWindowsTaskSchedule
 7 {
 8     /// <summary>
 9     /// 具体服务逻辑实现
10     /// </summary>
11     public class Test : IJob
12     {
13         public void Excute()
14         {
15             //throw new NotImplementedException();
16             FileStream fs = new FileStream(ConfigurationManager.AppSettings["LogFile"].ToString(), FileMode.OpenOrCreate, FileAccess.Write);
17             StreamWriter sw = new StreamWriter(fs);
18             sw.BaseStream.Seek(0, SeekOrigin.End);
19             sw.WriteLine(string.Format(""MyWindowsTaskSchedule" Windows Service Run At {0} 
", DateTime.Now.ToString()));
20             sw.Flush();
21             sw.Close();
22             fs.Close();
23         }
24 
25         public void Init()
26         {
27             //throw new NotImplementedException();
28         }
29 
30         public void OnError(Exception ex)
31         {
32             //throw new NotImplementedException();
33         }
34     }
35 }

主程序入口调用服务

 1 namespace MyWindowsTaskSchedule
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             var test = new Test();
 8             test.Excute();  //运行服务
 9         }
10     }
11 }

编译程序,bin目录的文件就是服务即将要跑的。

那么问题来了,我怎么跑?

我先附上一个截图再做简单说明。

C#使用定时任务框架Windows.TaskSchedule.exe安装控制台应用程序创建的Windows服务

Bin目录:存放控制台编译后的文件,包括配置文件。

Configs目录:两个配置文件Jobs.config和NLog.config

C#使用定时任务框架Windows.TaskSchedule.exe安装控制台应用程序创建的Windows服务

Jobs.config配置如下:

1 <?xml version="1.0" encoding="utf-8" ?>
2 <Jobs serverName="my-service" displayName="我的服务" description="我的服务描述">
3   <Job name="我的服务一" exePath="${basedir}BinMyWindowsTaskSchedule.exe" arguments="1" cornExpress="0/2 * * * * ?" expireSecond="300" />
4 </Jobs>

serverName:服务在任务管理器中显示的名称

displayName:服务在服务列表中显示的名称

Job节点:各个任务的具体配置,name建议唯一,exePath任务路径,cornExpress任务执行频率

实际应用中,我们需要修改Jobs.config,NLog.config可以不动。

安装服务.bat

1 F:maiaimeiWindowsServiceMyWindowsTaskScheduleDemoWindows.TaskSchedule.exe install
2 F:maiaimeiWindowsServiceMyWindowsTaskScheduleDemoWindows.TaskSchedule.exe start
3 pause

卸载服务.bat

1 F:maiaimeiWindowsServiceMyWindowsTaskScheduleDemoWindows.TaskSchedule.exe stop
2 F:maiaimeiWindowsServiceMyWindowsTaskScheduleDemoWindows.TaskSchedule.exe uninstall
3 pause

以上两个批处理命令,Windows.TaskSchedule.exe前面一大串,请根据需要调整。

其他文件直接拷贝过来即可。

万事俱备后(上面那个Demo目录截图),我们以管理员身份运行“安装服务.bat”,如下:

C#使用定时任务框架Windows.TaskSchedule.exe安装控制台应用程序创建的Windows服务

然后,我们可以在服务列表及任务管理器中检查

C#使用定时任务框架Windows.TaskSchedule.exe安装控制台应用程序创建的Windows服务

如果运行“卸载服务.bat”,效果如下:

C#使用定时任务框架Windows.TaskSchedule.exe安装控制台应用程序创建的Windows服务

源码及Demo下载:https://github.com/maiaimei/WindowsTaskSchedule

参考网址:

开源一个windows下的定时任务框架,简单粗暴好用。