【C#Windows 服务】 《三》Timer设置
一、工具:
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设置的更多相关文章
- vs 2010创建Windows服务定时timer程序
vs 2010创建Windows服务定时timer程序: 版权声明:本文为搜集借鉴各类文章的原创文章,转载请注明出处: http://www.cnblogs.com/2186009311CFF/p/ ...
- Windows服务中用Timer和线程两种方式来执行定时任务
在Service服务文件夹下新建Windows服务 - TestService
- MongoDB安装并设置为windows服务以使其开机自启
在MongoDB的官方下载windows平台的压缩zip文件,地址:https://www.mongodb.org/dr/fastdl.mongodb.org/win32/mongodb-win32- ...
- 如何用.NET创建Windows服务
我们将研究如何创建一个作为Windows服务的应用程序.内容包含什么是Windows服务,如何创建.安装和调试它们.会用到System.ServiceProcess.ServiceBase命名空间的类 ...
- 关于windows服务的编写/安装/与调试
前注: 首先,这篇文章是从网上转过来的,因为最近有个项目,需要编写一个Windows Service来定时执行程序,网上很容易找到了这篇文章,大概看了一下,文章讲的还是很详细的.不过这篇文章应该是.n ...
- WindowsService(Windows服务)开发步骤附Demo
1.打开VS,新建项目,选择Windows服务,然后设置目录及项目名称后点击确定. 2.展开Service1服务文件,编写service1.cs类文件,不是Service1[设计].然后修改OnSta ...
- WindowsService(Windows服务)开发步骤附Demo 【转】
转http://www.cnblogs.com/moretry/p/4149489.html 1.打开VS,新建项目,选择Windows服务,然后设置目录及项目名称后点击确定. 2.展开Service ...
- 【MongoDB】如何注册windows服务
一.为什么要注册windows服务 mongodb启动比较麻烦,每次都要cmd去开启.注册windows服务,可以设置开机启动,比较友好. 二.如何注册windows服务 1.安装mongodb 2. ...
- 制作Windows服务和安装程序(C#版)
http://blog.sina.com.cn/s/blog_5f4ffa170100vt2b.html 1.创建服务项目: 打开VS 2005 编程环境,在C#中新建Windows服务程序 2.将安 ...
- C#.NET 操作Windows服务(安装、卸载)
注意点: 1.安装时要请求到管理员权限. 2.卸载前,一定要停止掉Windows服务,否则需要重启或注销电脑.代码无法停止服务时,使用services.msc来停止. 开始: 1.新建一个名为&quo ...
随机推荐
- bzoj1305: [CQOI2009]dance跳舞(二分答案+网络流)
1305: [CQOI2009]dance跳舞 题目:传送门 题解: 一眼网络流基础建模...然后就GG了 二分答案+拆点建边+最大流判断: 把男女生拆为男1,男2,女1,女2 1.男1和男2还有女1 ...
- iOS对象方法和类方法的区别与调用方式
作为一个iOS程序员初学者,会搞不清楚对象方法和类方法的区别 -(void)duixiangfangfa ; +(void)leifangfa; - 代表实例方法,它在类的一个具体实例范围内执行,也就 ...
- [Codeforces 911F] Tree Destruction 解题报告(贪心)
题目链接: http://codeforces.com/contest/911/problem/F 题目大意: 给你一棵树,每次挑选这棵树的两个度数为1的点,加上他们之间的边数(距离),然后将其中一个 ...
- [poj 3349] Snowflake Snow Snowflakes 解题报告 (hash表)
题目链接:http://poj.org/problem?id=3349 Description You may have heard that no two snowflakes are alike. ...
- Ubuntu 18.04 关闭GUI
在安装显卡驱动时, 可能需要关闭GUI, 在终端中输入: sudo service gdm3 stop
- luogu 2679 子串
子串 史上最简短的一篇博客,毕竟看题解ac心疼我的kmp /* f[i][j][k][0/1]表示A的前i个,B的前j个,用到了k个子串,当前字符选或者不选. 所以f[0][0][0][0]的方案数为 ...
- Excel中将字符串中从右起第n个指定字符替换的方法
比如你想把www.baidu.com.cn中的倒数第二个”.”替换成@,则可以用: =SUBSTITUTE(A1,".","@",LEN(A1)-LEN(SUB ...
- SQL Server查询数据库空间分配情况、数据库备份信息
查询数据库空间分配情况: use master go create procedure dbo.proc_getdbspaceused as begin set nocount on create t ...
- oracle-常见的执行计划(一)
一.表访问方式 CBO基础概念中有讲到,访问表的方式有两种:全表扫描和ROWID扫描. 全表扫描的执行计划:TABLE ACCESS FULL ROWID扫描对应执行计划:TABLE ACCESS B ...
- App.config:配置系统未能初始化的异常
如上图所示:App.config文件是这样配置的,在后台代码”ISchedulerFactory scheduler = new StdSchedulerFactory();“中抛出了异常 经网上查资 ...