"C#windows Service" "Three" timer settings

Source: Internet
Author: User

"C#windows Service" "Three" Time settings

Directory:

1.beginning of "C#windows service" "One "

2."C#windows Service" "II" INI configuration file

3."C#windows Service" "Three" timer settings

First, tools:

Vs2015+net Framework4.5.

Second, Operation:

1, Timer settings:

2. Log code:

Third, the code:

1. Log Code:

1 /// <summary>2         ///Logging of windowns services3         /// </summary>4         /// <param name= "DbLog" ></param>5          Public Static voidWritedblogfile (stringDbLog)6         {7             //string logfilename = HttpContext.Current.Server.MapPath ("/log") + "/log_" + DateTime.Now.ToString ("yyyy-mm-dd") + " . txt ";8             stringLogFileName = AppDomain.CurrentDomain.BaseDirectory +"/log_"+ DateTime.Now.ToString ("YYYY-MM-DD") +". txt";9 System.IO.StreamWriter write;TenWrite =NewSystem.IO.StreamWriter (LogFileName,true, System.Text.Encoding.Default); OneWrite. Basestream.seek (0, System.IO.SeekOrigin.End); AWrite. AutoFlush =true; -             if(NULL!=write) -             { the                 Lock(write) -                 { -                     //write. WriteLine ("—————————————— windowns service logging started ————————————————"); -Write. WriteLine ("log contents of the Windowns service:"+dbLog); +Write. WriteLine ("windowns logging time for service:"+DateTime.Now); -                     //write. WriteLine ("———————————————— logging end of the —————————————— windowns Service"); + write. Flush (); A                 } at             } - write. Close (); -Write =NULL; -}

2. Timer Setup code:

1 usingCommon;2 usingSystem;3 usingSystem.Collections.Generic;4 usingSystem.ComponentModel;5 usingSystem.Data;6 usingSystem.Diagnostics;7 usingSystem.Linq;8 usingsystem.serviceprocess;9 usingSystem.Text;Ten usingSystem.Threading; One usingSystem.Threading.Tasks; A  - namespaceWindowsservicedb - { the      Public Partial classService1:servicebase -     { -  -System.Timers.Timer Timer;//Timer +          PublicService1 () -         { + InitializeComponent (); A         } at  -         protected Override voidOnStart (string[] args) -         { -Thread thread =NewThread (Delegate () -             { -                 Try in                 { -                      for(inti =0; I <Ten; i++) to                     { +                         //utils.writedblogfile ("—————————————— windowns service logging started ————————————————"); -  theTimer =NewSystem.Timers.Timer (); *Timer. Interval = the; $Timer. Elapsed + =NewSystem.Timers.ElapsedEventHandler (timer_elapsed);Panax NotoginsengTimer. AutoReset =true;//Whether the setting is executed once (false) or always executed (true); -Timer. Enabled =true;//whether to execute the System.Timers.Timer.Elapsed event; theUtils.writedblogfile ("Service start time:"+DateTime.Now); +                     } A                 } the                 Catch(Exception ex) +                 { -  $Utils.writedblogfile ("Service failed to start"+ex); $                 } -             }); -             //utils.writedblogfile ("———————————————— logging end of the —————————————— windowns Service"); theThread. Name ="Threading Test 1"; -Thread. IsBackground =true;Wuyi thread. Start (); the         } -  Wu         protected Override voidOnStop () -         { AboutTimer. Enabled =false; $Utils.writedblogfile ("End of service time:"+DateTime.Now); -         } -  -         Private voidTimer_elapsed (Objectsender, System.Timers.ElapsedEventArgs e) A         { +             //Perform Actions theUtils.writedblogfile ("The service starts recording time:"+DateTime.Now); -  $         } the     } the}

Iv. Summary:

Keep track of every bit of the day, code good every line

"C#windows Service" "Three" timer settings

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.