c#之添加window服务(定时任务)
本文讲述使用window服务创建定时任务
1.如图,新建项目,windows桌面->windows服务

2.如图,右键,添加安装程序

3.在下图安装程序 serviceInstaller1 上右键,修改serviceName和Description

4.如图,在 serviceProcessInstaller 上右键,修改 Account 为 LocalSystem

5.在Service1中 添加代码
public partial class Service1 : ServiceBase
{
//记录到event log中,地址是 C:\Windows\System32\winevt\Logs (双击查看即可,文件名为MyNewLog)
private static EventLog eventLog1;
private int eventId = ; public Service1()
{
InitializeComponent(); eventLog1 = new System.Diagnostics.EventLog();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
} /// <summary>
/// 启动服务
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart.");
log("In OnStart."); // Set up a timer that triggers every minute. 设置定时器
Timer timer = new Timer();
timer.Interval = ; // 60 seconds 60秒执行一次
timer.Elapsed += new ElapsedEventHandler(this.OnTimer);
timer.Start();
} /// <summary>
/// 停止服务
/// </summary>
protected override void OnStop()
{
eventLog1.WriteEntry("In OnStop.");
log("In OnStop.");
} /// <summary>
/// 继续服务
/// </summary>
protected override void OnContinue()
{
eventLog1.WriteEntry("In OnContinue.");
log("In OnContinue.");
} /// <summary>
/// 定时器中定时执行的任务
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
public void OnTimer(object sender, ElapsedEventArgs args)
{
// TODO: Insert monitoring activities here.
eventLog1.WriteEntry("Monitoring the System", EventLogEntryType.Information, eventId++);
log("the timer");
} /// <summary>
/// 记录到指定路径:D:\log.txt
/// </summary>
/// <param name="message"></param>
private static void log(string message)
{
using (FileStream stream = new FileStream("D:\\log.txt", FileMode.Append))
using(StreamWriter writer=new StreamWriter(stream))
{
writer.WriteLine($"{DateTime.Now}:{message}");
}
} }
结构如图:

6.右键生成解决方案

7.打开项目,复制bin目录到C盘下新建的 windowServiceTest 文件夹的 windowService_Hello文件夹下,另外复制 C:\Windows\Microsoft.NET\Framework\v4.0.30319 下的 InstallUtil.exe 到 windowServiceTest 文件夹下;如图


8.安装服务
打开cmd (以管理员身份),并且进入windowServiceTest 文件夹下
安装服务:
InstallUtil.exe C:\windowServiceTest\windowService_Hello\bin\Debug\WindowService_HelloWorld.exe

效果图:

9.打开服务管理器,启动MyService服务,并且等待几分钟,然后卸载服务
卸载服务:
InstallUtil.exe -u C:\windowServiceTest\windowService_Hello\bin\Debug\WindowService_HelloWorld.exe

10.检验是否有效果
在D盘发现log.txt文件,打开如下

event log 所在如图

可见,window服务上的定时任务已生效
参考网址
https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer
c#之添加window服务(定时任务)的更多相关文章
- 使用winform程序控制window服务的操作
继上篇 c#之添加window服务(定时任务) 基础之上, 这篇文章主要讲述,使用winform程序来控制window服务的安装,启动,停止,卸载等操作 1.在同一个解决方案添加winform项目,如 ...
- 创建 window service 定时任务
参考文章:http://www.cnblogs.com/jack-liang/archive/2011/05/20/2051743.html 前段时间做过一个项目,前端系统提供添加定时任务,后端系统要 ...
- 自定义Window 服务
自定义window 服务 开发到使用的流程: 1.完成对应的代码之后(代码在底下),右键MyService.cs 添加安装程序 2.添加window服务安装程序打开Service1.cs[设计]页面, ...
- C#编写window服务,一步一步(1)
Window服务是啥,这里就不废话了,如何用在哪里用也不废话了,这里我这篇文章只是详述了我在vs2012中创建window服务的经过,希望对你有所帮助. 另外:我在编写服务过程中参考了 Profess ...
- C# 编写Window服务基础(一)
一.Windows服务介绍: Windows服务以前被称作NT服务,是一些运行在Windows NT.Windows 2000和Windows XP等操作系统下用户环境以外的程序.在以前,编写Wind ...
- C# 编写短信发送Window服务
我们做项目过程中,一般都会有发送短信的需求.最常见的就是户注册或者登录时发送短信验证码.不同类型的短信发送,我们都可以放到到一张短信表中,然后通过一个定时的作业去执行短信发送.而定时作业的执行,我们就 ...
- window服务创建
第一步:创建服务 第二步:在Service1.cs视图中 右键 选择”添加安装程序” 这里要注意几个细节 设置上面的属性 这两个分别有属性,具体网上查使用方式 3 实例代码编写 主要下面几个方法 pr ...
- C#在window服务配置Log4Net.dll
1.使用背景: C#window服务下添加一个日志记录程序集(Log4Net.dll) 2.添加和使用步骤如下: 一.下载并引入Log4Net.dll程序集到项目中 下载地址:http://loggi ...
- 创建一个MongoDB数据库再到配置成Window服务再设置用户名密码
1.安装MongoDB数据在官网下载安装 然后在C盘找到C:\Program Files\MongoDB\Server\4.0\bin这个可执行目录 使用cmd进入到这: 2.在C盘根目录创建一个名为 ...
随机推荐
- PlayJava Day007
今日所学: /* 2019.08.19开始学习,此为补档. */ 1.String类 实例化:①String name1 = "张三" ; ②String name2 = new ...
- Flask 教程 第四章:数据库
本文翻译自 The Flask Mega-Tutorial Part IV: Database 在Flask Mega-Tutorial系列的第四部分,我将告诉你如何使用数据库. 本章的主题是重中之重 ...
- JDK1.8新特性——使用新的方式遍历集合
JDK1.8新特性——使用新的方式遍历集合 摘要:本文主要学习了在JDK1.8中新增的遍历集合的方式. 遍历List 方法: default void forEach(Consumer<? su ...
- PLC与外接按钮开关接线方法图解
一个电机控制电路如图1所示,电路中使用常开按钮启动电机,用常闭按钮停止电机运行,图1中KM是控制电机电源的继电器.这样的电路若是使用PLC时的外接线图如图2所示.同时为使PLC运行,在PLC中输入由图 ...
- Java正则表达式详细解析
元字符 正则表达式使用一些特定的元字符来检索.匹配和替换符合规则的字符串 元字符:普通字符.标准字符.限定字符(量词).定位字符(边界字符) 正则表达式引擎 正则表达式是一个用正则符号写出来的公式 程 ...
- uni-app学习(四)好用的插件2
1. uni-app学习(四)好用的插件2 1.1. 树形结构 点击这里 1.2. 下拉刷新上拉加载组件 如果想把下拉上拉做成自定义的,更加好看,可以使用这个插件 地址这里 举个例子 1.3. 浮动键 ...
- UML类图和用例图
软件体系结构的多视图 kruchten提出了软件体系结构的4+1视图模型,其中用例图位于中心位置(4+1视图中的1). 逻辑视图 一种静态建模视图 进程视图 一种并发进程或任务视图 开发视 ...
- Java垃圾收集器——Serial,Parallel,CMS,G1收集器概述
1.概述 Java应用启动的时候,除了配置Xms以及Xmx参数(Xmx:InitialHeapSize, Xms:MaxHeapSize),还需要选择合适的垃圾收集器. 截止Jdk1.8,共提供了7款 ...
- [转]Oracle 11g RAC SCAN ip的原理及配置
原文地址:http://tiany.blog.51cto.com/513694/1421917/ Oracle 11g RAC SCAN ip的原理及配置 Oracle 11g RAC网格即插即用 ...
- docker网络之(三)
docker4种网络 基于docker run创建容器时,可以使用--net选项指定容器的网络模式:Docker默认有以下4种网络模式: host模式,使用--net=host指定 container ...