博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
建立一个windows服务(可用于实现计划任务,事件监控..) .NET
阅读量:7238 次
发布时间:2019-06-29

本文共 4627 字,大约阅读时间需要 15 分钟。

什么是windows服务?

一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序。Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就能开始运行了,它必须有特定的启动方式。这些启动方式包括了自动启动和手动启动两种。对于自动启动的Windows服务程序,它们在 Windows启动或是重启之后用户登录之前就开始执行了。只要你将相应的Windows服务程序注册到服务控制管理器(Service Control Manager)中,并将其启动类别设为自动启动就行了。而对于手动启动的Windows服务程序,你可以通过命令行工具的NET START 命令来启动它,或是通过控制面板中管理工具下的服务一项来启动相应的Windows服务程序(见图1)。同样,一个Windows服务程序也不能像一般的应用程序那样被终止。因为Windows服务程序一般是没有用户界面的,所以你也要通过命令行工具或是下面图中的工具来停止它,或是在系统关闭时使得 Windows服务程序自动停止。因为Windows服务程序没有用户界面,所以基于用户界面的API函数对其是没有多大的意义。为了能使一个 Windows服务程序能够正常并有效的在系统环境下工作,程序员必须实现一系列的方法来完成其服务功能。Windows服务程序的应用范围很广,典型的 Windows服务程序包含了硬件控制、应用程序监视、系统级应用、诊断、报告、Web和文件系统服务等功能。

通过时间控件 定时检测实现执行任务的

自动会在相应的工程(如WinService1)中创建一个Service1.cs的文件(将Service1.cs改名为ScheduleService.cs),选中该文件,查看代码,

using System; using System.ServiceProcess; using System.Timers; namespace WinService1 {
public partial class ScheduleService : ServiceBase {
#region 属性定义 /// /// 定时器 /// Timer Timer1; #endregion public ScheduleService() {
InitializeComponent(); Timer1 = new Timer(); ServiceName = "ScheduleService"; CanStop = true; CanShutdown = true; CanPauseAndContinue = true; AutoLog = true; Timer1.Elapsed += Timer1_Elapsed; Timer1.Interval = 1000; Timer1.Start(); } /// /// 达到间隔时间 /// void Timer1_Elapsed(object sender, ElapsedEventArgs e) {
if (e.SignalTime.Second == 0)//每分钟执行一次 {
//检测配置文件中,是否有需要执行的任务 if (false) {
WriteLog("文件1", "已经检测过了,有要执行的任务:***,并已安排执行"); } else WriteLog("文件1", "已经检测过了,没有要执行的任务"); } } protected override void OnStart(string[] args) {
base.OnStart(args); WriteLog("文件1", "WinService1.OnStart" + " 服务启动"); } protected override void OnStop() {
base.OnStop(); WriteLog("文件1", "WinService1.OnStop" + " 服务停止"); } #region Log private static string logPath = string.Empty; /// /// 保存日志的文件夹 /// public static string LogPath {
get {
if (logPath == string.Empty) {
if (System.Web.HttpContext.Current == null) // Windows Forms 应用 logPath = AppDomain.CurrentDomain.BaseDirectory; else // Web 应用 logPath = AppDomain.CurrentDomain.BaseDirectory + @"bin"; } return logPath; } set { logPath = value; } } /// /// 写日志 /// /// 文件名,如:Info /// 日志内容 public static void WriteLog(string logFile, string msg) {
try {
System.IO.StreamWriter sw = System.IO.File.AppendText( LogPath + logFile + " " + DateTime.Now.ToString("yyyyMMdd") + ".Log" ); sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg); sw.Close(); } catch { } } #endregion } }

 

新建类MyInstaller.cs

using System; using System.Configuration.Install; using System.ServiceProcess; using System.ComponentModel; namespace WinService1 {
[RunInstaller(true)] public partial class MyInstaller : Installer {
private ServiceInstaller sInstall; private ServiceProcessInstaller sProcessInstall; public MyInstaller() {
sInstall = new ServiceInstaller(); sProcessInstall = new ServiceProcessInstaller(); sProcessInstall.Account = ServiceAccount.LocalSystem; sInstall.StartType = ServiceStartMode.Automatic; sInstall.ServiceName = "ScheduleService"; //这个服务名必须和步骤1中的服务名相同。 sInstall.DisplayName = "任务计划 服务"; sInstall.Description = "这是该任务计划服务的描述.."; Installers.Add(sInstall); Installers.Add(sProcessInstall); } } }

 

在Program.cs中

using System.ServiceProcess; namespace WinService1 {
static class Program {
/// /// 应用程序的主入口点。 /// static void Main() {
ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new ScheduleService() }; ServiceBase.Run(ServicesToRun); //在运行中,将当前目录转向WinService1.exe所在的目录,(盘符直接切换, cd 切换文件夹) //然后输入 F:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe WinService1.exe //则在服务中可以看到新增了一项服务 //另外,删除服务的方法如下: //在运行中,将当前目录转向WinService1.exe所在的目录, //sc delete myservice (myservice为当前要删除的服务名),即可删除该服务 } } }

生成一下该工程,然后

根据Program.cs中的相关注释,在 管理>服务中添加上 新的服务,然后启动;

在调试>附加到进程 中

此时即可调试服务程序

转载地址:http://ftofm.baihongyu.com/

你可能感兴趣的文章
verilog语法实例学习(1)
查看>>
杭电2079
查看>>
Delphi单元--共50个函数
查看>>
(原創) 如何將Quartus II 7.2所建立的SOPC系統升級到Quartus II 8.0? (SOC) (Quartus II) (SOPC Builder)...
查看>>
如果不使用 Navigate2 的方法去访问现有页面,如何将JS写到该浏览器中???
查看>>
Form表单中method="post/get'的区别
查看>>
'telnet' is not recognized as an internal or external command
查看>>
什么是 MIME Type?
查看>>
c#设计模式第一天
查看>>
Jquery.Form和jquery.validate 的使用
查看>>
c++ string 和wstring 之间的互相转换函数
查看>>
mathematica练习程序(图像取反)
查看>>
mssql和mysql区别
查看>>
Java安全通信概述
查看>>
C++ vector类型要点总结
查看>>
在global里捕获黄页并写入异常日志库
查看>>
如何使用Transact-SQL进行事务处理[示例]
查看>>
cxgrid在内置右键菜单的后面增加菜单项
查看>>
第三届云计算大会 - RackSpace CTO John Engates:开放云的必要性(转载)
查看>>
步步为营 C# 技术漫谈 三、公共语言运行库(CLR)
查看>>