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盘根目录创建一个名为 ...
随机推荐
- PHP+Ajax点击加载更多列表数据实例
一款简单实用的PHP+Ajax点击加载更多列表数据实例,实现原理:通过“更多”按钮向服务端发送Ajax请求,PHP根据分页参数查询将最新的几条记录,数据以JSON形式返回,前台Query解析JSON数 ...
- CSS filter滤镜试玩
1.模糊(blur). 用法:给相应元素设置高斯模糊,传入的px数值越大越模糊. 2.亮度(brightness). 用法:给元素设置亮度,0%为全黑,100%为元素原始亮度,>100%表示会比 ...
- 完美解决linux下vim在终端不能用鼠标复制的问题
在vim 中输入 :set mouse=r 就行了,还不行的话 :set mouse=v
- javascript常用数据验证函数
正则表达式日期验证函数 function CheckDate(str){ //在JavaScript中,正则表达式只能使用"/"开头和结束,不能使用双引号 ...
- 0基础入门学习Python(第5章)
列表,元组和字符串 5.1 列表:一个打了激素的数组 有时候可能需要将一些相互之间有关联的数据保存在一起,这个就叫数组.Python将其称为列表. 5.1.1 创建列表 >>> [1 ...
- 【转载】Android开发中巧用Activity和Fragment
1.Activity的生命周期 1)多个Activity组成Activity栈,当前活动位于栈顶.我们先来看看各种Activity基类的类图: 当Activity类定义出来之后,这个Activity何 ...
- Microsoft.Extensions.DependencyInjection 阅读笔记
一. 关于IServiceCollection接口的设计 public interface IServiceCollection : IList<ServiceDescriptor> { ...
- PHP比较两个数组的差异
array_diff($arr, $arr1); //比较数组差异 $arr = [1,2,3,4]; $arr1 = [1,2,3]; $diff = array_diff($arr, $arr1) ...
- pycharm 字符编码错误处理
如果在MySQL创建表空间时指定了他的字符集为utf8 但是插入数据后 在pycharm查看却出现字符乱码 怎么解决呢 ? 首先进入cmd 页面 输入 chcp 65001 (意思是指定cmd所有字 ...
- CodeForces - 1253C(思维+贪心)
题意 https://vjudge.net/problem/CodeForces-1253C n个糖果,一天最多吃m个糖果,每个糖果有个值a[i],第d天会变成d*a[i],问吃k(k=1~n)个糖果 ...