[C # Windows Service] Timer settings in "3,

Source: Internet
Author: User

[C # Windows Service] Timer settings in "3,

[C # Windows Service] "3" Time settings

Directory:

1.[C # Windows Service] Getting started with "1"

2.[C # Windows Service] INI configuration file

3.[C # Windows Service] Timer settings in "3"

 

I. tools:

VS2015 + NET Framework4.5.

 

Ii. Operations:

1. timer settings:

 

2. Log Code:

 

Iii. Code:

1. Log Code:

1 /// <summary> 2 // Windowns Service Log Record 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 ("-------------- the log record of the Windowns service starts ----------------"); 18 write. writeLine ("Windowns Service Log Record Content:" + dbLog); 19 write. writeLine ("Windowns service Logging Time:" + DateTime. now); 20 // write. writeLine ("-------------- the log record of the Windowns service ends ----------------"); 21 write. flush (); 22} 23} 24 write. close (); 25 write = null; 26}

 

2. Timer setting code:

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; // 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 ("-------------- start of Windowns service logging ----------------"); 33 34 timer = new System. timers. timer (); 35 timer. interval = 3000; 36 timer. elapsed + = new System. timers. elapsedEventHandler (Timer_Elapsed); 37 timer. autoReset = true; // set whether to execute once (false) or always execute (true); 38 timer. enabled = true; // whether to execute System. timers. timer. elapsed event; 39 Utils. writeDBLogFile ("service start Time:" + DateTime. now); 40} 41} 42 catch (Exception ex) 43 {44 45 Utils. writeDBLogFile ("service startup failed" + ex); 46} 47}); 48 // Utils. writeDBLogFile ("-------------- the log record of the Windowns service ends ----------------"); 49 thread. name = "thread Test 1"; 50 thread. isBackground = true; 51 thread. start (); 52} 53 54 protected override void OnStop () 55 {56 timer. enabled = false; 57 Utils. writeDBLogFile ("service end Time:" + DateTime. now); 58} 59 60 private void Timer_Elapsed (object sender, System. timers. elapsedEventArgs e) 61 {62 // execute the operation 63 Utils. writeDBLogFile ("service start record Time:" + DateTime. now); 64 65} 66} 67}

 

Iv. Summary:

 

Record every day, code every line of code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.