【C#Windows 服务】 《三》Timer设置

【C#Windows 服务】 《三》Time设置

目录:

1.【C#Windows 服务】 《一》初入门

2.【C#Windows 服务】 《二》INI配置文件

3.【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 }
复制代码

 

四、总结:

 

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

 

posted @   ℃7O八落~的点滴  阅读(2015)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示