一、工具:

VS2015+NET Framework4.5。

二、操作:

1、计时器设置:

2、日志代码:

三、代码:

1、日志代码:

 1 /// <summary>
2 /// Windowns服务的日志记录
3 /// </summary>
4 /// <param name="dbLog"></param>
5 public static void WriteDBLogFile(string dbLog)
6 {
7 // string logfilename = HttpContext.Current.Server.MapPath("/Log") + "/log_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
8 string logfilename = AppDomain.CurrentDomain.BaseDirectory + "/log_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
9 System.IO.StreamWriter write;
10 write = new System.IO.StreamWriter(logfilename, true, System.Text.Encoding.Default);
11 write.BaseStream.Seek(0, System.IO.SeekOrigin.End);
12 write.AutoFlush = true;
13 if (null != write)
14 {
15 lock (write)
16 {
17 //write.WriteLine("——————————————Windowns服务的日志记录开始————————————————");
18 write.WriteLine("Windowns服务的日志记录内容:" + dbLog);
19 write.WriteLine("Windowns服务的日志记录时间:" + DateTime.Now);
20 //write.WriteLine("——————————————Windowns服务的日志记录结束————————————————");
21 write.Flush();
22 }
23 }
24 write.Close();
25 write = null;
26 }

2、Timer设置代码:

 1 using Common;
2 using System;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Data;
6 using System.Diagnostics;
7 using System.Linq;
8 using System.ServiceProcess;
9 using System.Text;
10 using System.Threading;
11 using System.Threading.Tasks;
12
13 namespace WindowsServiceDB
14 {
15 public partial class Service1 : ServiceBase
16 {
17
18 System.Timers.Timer timer; //计时器
19 public Service1()
20 {
21 InitializeComponent();
22 }
23
24 protected override void OnStart(string[] args)
25 {
26 Thread thread = new Thread(delegate ()
27 {
28 try
29 {
30 for (int i = 0; i < 10; i++)
31 {
32 // Utils.WriteDBLogFile("——————————————Windowns服务的日志记录开始————————————————");
33
34 timer = new System.Timers.Timer();
35 timer.Interval = 3000;
36 timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
37 timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
38 timer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
39 Utils.WriteDBLogFile("服务启动Time:" + DateTime.Now);
40 }
41 }
42 catch (Exception ex)
43 {
44
45 Utils.WriteDBLogFile("服务启动失败" + ex); ;
46 }
47 });
48 //Utils.WriteDBLogFile("——————————————Windowns服务的日志记录结束————————————————");
49 thread.Name = "线程测试1";
50 thread.IsBackground = true;
51 thread.Start();
52 }
53
54 protected override void OnStop()
55 {
56 timer.Enabled = false;
57 Utils.WriteDBLogFile("服务结束Time:" + DateTime.Now);
58 }
59
60 private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
61 {
62 //执行操作
63 Utils.WriteDBLogFile("服务开始记录Time:" + DateTime.Now);
64
65 }
66 }
67 }

四、总结:

记录每一天的点滴,码好每一行的代码

【C#Windows 服务】 《三》Timer设置的更多相关文章

  1. vs 2010创建Windows服务定时timer程序

    vs 2010创建Windows服务定时timer程序: 版权声明:本文为搜集借鉴各类文章的原创文章,转载请注明出处:  http://www.cnblogs.com/2186009311CFF/p/ ...

  2. Windows服务中用Timer和线程两种方式来执行定时任务

    在Service服务文件夹下新建Windows服务 - TestService

  3. MongoDB安装并设置为windows服务以使其开机自启

    在MongoDB的官方下载windows平台的压缩zip文件,地址:https://www.mongodb.org/dr/fastdl.mongodb.org/win32/mongodb-win32- ...

  4. 如何用.NET创建Windows服务

    我们将研究如何创建一个作为Windows服务的应用程序.内容包含什么是Windows服务,如何创建.安装和调试它们.会用到System.ServiceProcess.ServiceBase命名空间的类 ...

  5. 关于windows服务的编写/安装/与调试

    前注: 首先,这篇文章是从网上转过来的,因为最近有个项目,需要编写一个Windows Service来定时执行程序,网上很容易找到了这篇文章,大概看了一下,文章讲的还是很详细的.不过这篇文章应该是.n ...

  6. WindowsService(Windows服务)开发步骤附Demo

    1.打开VS,新建项目,选择Windows服务,然后设置目录及项目名称后点击确定. 2.展开Service1服务文件,编写service1.cs类文件,不是Service1[设计].然后修改OnSta ...

  7. WindowsService(Windows服务)开发步骤附Demo 【转】

    转http://www.cnblogs.com/moretry/p/4149489.html 1.打开VS,新建项目,选择Windows服务,然后设置目录及项目名称后点击确定. 2.展开Service ...

  8. 【MongoDB】如何注册windows服务

    一.为什么要注册windows服务 mongodb启动比较麻烦,每次都要cmd去开启.注册windows服务,可以设置开机启动,比较友好. 二.如何注册windows服务 1.安装mongodb 2. ...

  9. 制作Windows服务和安装程序(C#版)

    http://blog.sina.com.cn/s/blog_5f4ffa170100vt2b.html 1.创建服务项目: 打开VS 2005 编程环境,在C#中新建Windows服务程序 2.将安 ...

  10. C#.NET 操作Windows服务(安装、卸载)

    注意点: 1.安装时要请求到管理员权限. 2.卸载前,一定要停止掉Windows服务,否则需要重启或注销电脑.代码无法停止服务时,使用services.msc来停止. 开始: 1.新建一个名为&quo ...

随机推荐

  1. js判断是对象还是集合

    /*============================================ 函数功能:对返回数据中的列表数据进行非空处理 ============================== ...

  2. 移动端fixed后 横竖屏切换时上部或下部出现空隙问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. JAR包放在WEB-INF/lib子目录报ClassNotFoundException解决方案

    对于Java Web应用依赖的jar包,我们通常会放到WEB-INF/lib目录下,但是笔者喜欢把不同框架的jar包放在不同的子目录下,例如新建一个struts目录存放struts框架的jar包等. ...

  4. nginx的location 匹配的规则问题

    正则解释: ~ #匹配一个正则匹配,区分大小写~* #匹配一个正则,不区分大小写^~ #普通字符匹配,如果该选择匹配不匹配别的选项,一般用来匹配目录= #精确匹配 匹配案例:location = / ...

  5. Docker学习笔记(1)----认识Docker

    1. 什么Docker? Docker是一个能把开发的应用程序自动部署到容器的引擎,它使用go语言编写的开源引擎,它在github上面个的地址为:https://github.com/docker/d ...

  6. 优动漫PAINT-草地教程

    非常实用的草地教程,是场景控们绝对要学会的绘画技巧~更有配套草地笔刷~让场景绘画更简易~ 教程是简单,呃.... 没有优动漫PAINT软件肿么办? 别着急,╭(╯^╰)╮ 小编给你送来了 齐全的哟? ...

  7. 小型ceph集群的搭建

    了解ceph DFS(distributed file system)分布式存储系统,指文件系统管理的物理存储资源,不一定直接连接在本地节点上,而是通过计算机网络与节点相连,众多类别中,ceph是当下 ...

  8. 洛谷P3355 骑士共存问题 二分图_网络流

    Code: #include<cstdio> #include<cstring> #include<queue> #include<vector> #i ...

  9. 洛谷P3254 圆桌问题 网络流_二分图

    Code: #include<cstdio> #include<algorithm> #include<vector> #include<queue> ...

  10. Markdown语法简记

    目录 一.标题 1. 六个级别的标题 2. 主.副两级标题 二.根据标题生成文档结构大纲 三.字体 1. 斜体 2. 粗体 3. 倾斜加粗 4. 行首缩进 5. 删除线 四.引用块 五.代码块 1. ...