一、工具:

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. hpc-ai比赛相关资料

    http://follitude.com/blog/Use-RDMA-Emulation-in-Software-Using-SoftiWARP/ https://www.reflectionsoft ...

  2. 11.使用boostregex遭遇无法打开libboost_regex-vc120-mt-sgd-1_62.lib的问题

    通过Boost库可以在C++项目中使用正则表达式,配置好环境后链接过程出现”无法打开libboost_regex-vc120-mt-sgd-1_62.lib”的错误.  原因是按照官方生成lib的方法 ...

  3. Python学习之基本概念

    1.Python是一种解释型语言.Python解释器通过“一次执行一条语句”的方式执行程序的. 2.Python用空白来组织程序,不像R等用大括号. 3.# 是Python的注释符号. 4.变量是按引 ...

  4. DISM

    C:\WINDOWS\system32>DISM /Online /Cleanup-image /RestoreHealth 部署映像服务和管理工具版本: 10.0.16193.1001 映像版 ...

  5. Sublime Text 3 注册码 激活码 版本号 Build 3143

    —– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...

  6. SQL 的stuff函数

    1.作用 删除指定长度的字符,并在指定的起点处插入另一组字符. 2.语法 STUFF ( character_expression , start , length ,character_expres ...

  7. 通过curl获取网页访问时间

    curl -w %{time_namelookup}:%{time_connect}:%{time_starttransfer}:%{time_total}:%{speed_download}&quo ...

  8. thread.h

    https://github.com/wookayin/pintos/blob/master/src/threads/thread.h #ifndef THREADS_THREAD_H   #defi ...

  9. 爬虫来啦!Day91

    # 一.爬虫# 1.基本操作# 排名爬虫刷票# 抽屉网的所有发布新闻点赞# 自动化程序模拟用于的日常操作# 投票的机制是利用cookies,禁用cookies模式# 自定义的异步IO模块就是Socke ...

  10. URL正则

    现有需求 表单填写域名只能填写 baseURL 或者 baseURL+端口 不带协议 否则为不合法 String url1 = ".com:90"; String url2 = & ...