一、工具:

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. App 签名过期或泄露怎么办?别担心,Google 已经给出解决方案!

    一.序 在将 App 发布到市场之前,很重要的一个步骤就是为 APK 进行签名,大部分时候,这个操作隐藏在了打包的流程中,而不被我们注意到. 签名的作用,除了证明 App 的所有权之外,还可以帮助 A ...

  2. Android 6.0 开发人员对系统权限的使用与练习(Permissions Best Practices)

    Permissions Best Practices 在安装的过程中,用户非常easy忽略权限请求. 假设一个用户相应用感觉沮丧或者操心泄漏个人信息,那么这些用户就会不用他或者卸载它. 怎样规避这个问 ...

  3. 【linux】——centos 分辨率配置

    用过centos的朋友肯定知道centos在默认安装的时候显示器的分辨率只有800*600,但是我们想把改成1024*768或者更大,怎么办呢,我也是试过了才知道,首先打开系统-管理-显示-硬件-显示 ...

  4. [jzoj 5930] [NOIP2018模拟10.26】山花 解题报告 (质因数分类)

    题目链接: http://172.16.0.132/senior/#contest/show/2538/2 题目: 小S决定从某一个节点$u$开始对其子树中与$u$距离小于$K$的节点代表的花树进行采 ...

  5. handsontable在线编辑excel扩展功能-踩坑篇

    简述 先说一下背景,之所以封装handsontable插件,是因为公司要实现在线编辑导入excel文件的功能,然后我就找到了这个功能强大的插件handsontable. 具体功能 除了handsont ...

  6. (转载)ScratchView

    首页 我的管理 客户端 登录注册 首页 安卓组件中心 安卓代码分享 Swift专区 HTML5移动开发 视频中心 小程序 名称: ScratchView 作者: doliangzhe 来源: gith ...

  7. shell-4.bash的变量:用户自定义变量

    目录 内容

  8. Linux rm删除大批量文件遇到 Argument list too long

    在使用rm删除大批量文件时,有可能会遭遇“参数列太长”(Argument list too long)的问题.如下所示   [oracle@DB-Server bdump]$ rm -v epps_q ...

  9. 线程状态与tcb、线程的生命周期

    struct tcb { u32_t status; struct reg_context thread_context; void *stack; struct thread_info thread ...

  10. ZBrush中常用3D笔触效果

    3D笔触共有6种绘制方式,分别为Dots(点).Drag Rect(拖拉矩形).Freehand(徒手绘制).Color Spray(彩色喷溅).Spray(喷溅)和Drag Dot(拖拽斑点). 1 ...