【C#Windows 服務】 《三》Timer設定

來源:互聯網
上載者:User

標籤:director   代碼   roc   text   ice   dia   ack   class   das   

一、工具:

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 WindowsServiceDB14 {15     public partial class Service1 : ServiceBase16     {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                 try29                 {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設定

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.