C # create a Windows service series 2: demonstrate a periodically executed windows service)

Source: Internet
Author: User

Series 1: Create a Windows service that is installable, can be started, can be stopped, and can be uninstalled (downmoon original)

Series 2: demonstrate a regularly executed windows service and debug (windows service) (downmoon)

Series 3: windows service series 3-create a windows service with a controllable Interface

 

I,Some people often ask how to make the program run automatically on a regular basis.?
Apart from system tasks and SQL JOB/DTS, which can meet different user needs, we demonstrate how to build a simple windows serivce framework. The main function is to perform similar operations according to preset time intervals, such as crawling webpages, capturing weather forecasts, and sending mass emails.

Assume that the following code needs to be automatically executed on a regular basis:

Code
1. Get content # obtain content from region
2/** // <summary>
3 // obtain the content, by tony 2009.9, 16
4 // downmoon: 3w@live.cn
5 /// </summary>
6 public void GetContentByMethods (bool isDelteOldFile)
7 {
8 System. Text. StringBuilder builder = new System. Text. StringBuilder ();
9
10 try
11 {
12 DeleteOldFile # region DeleteOldFile
13 if (isDelteOldFile)
14 {
15 string path = Globals. BakPath;
16 if (System. IO. Directory. Exists (path ))
17 {
18 string [] strFiles = System. IO. Directory. GetFiles (path );
19 foreach (string strFile in strFiles)
20 {
21 System. IO. File. Delete (strFile );
22}
23}
24}
25 # endregion
26 My Methods # region My Methods
27 int tempCount = 0;
28 string temp = "Operation _ {0} was sucessfully executed at {1} by Operator _ {2 }! ";
29 string temp2 = string. Empty;
30 string filename = Globals. CreateXMLDir (Globals. BakPath) + Globals. StrTempDate + ". txt ";
31 for (int k = 0; k <Globals. TopCount; k ++)
32 {
33 temp2 = string. Empty;
34 // read data from the data table and do something like sending an email or capturing webpage content.
35 temp2 = string. format (temp, k, DateTime. now. toString ("yyyy-MM-dd HH: mm: ss. fff "), k) +" \ r \ n ";
36 builder. Append (temp2 );
37 tempCount ++;
38}
39 builder. Append ("total [" + tempCount + "] records was executed this time! ");
40 SaveFileResult (filename, builder. ToString ());
41 # endregion
42}
43 catch (Exception ex) {string s = ex. Message ;}
44}

Then, you only need to deploy the above Code in a windows service environment and deploy it as a windows service.
I will omit the detailed process! Source code after reading
Note:
1. SetupServer. bat, double-click to install the service
2. unInstallServer. bat. Double-click to uninstall the service.
32.16main file wsdemobytime.exe
4、configuration file wsdemobytime.exe. config (mainly configure the quantity, interval, data connection, etc)
Running Effect: Only one basic record file is generated. See figure;
  

  
  

  Ii. How to debug windows service ).

A simple process is provided here:

Assuming that we have already deployed windows service and found that we cannot run it as we imagined. In addition to the logs described in this series, we can also directly debug the process.

For detailed steps, see:

I. debugging-attach to process

  
  

Ii. set breakpoints and debug
  
  3. How can I modify and take effect of windows service configuration?

Note that the windows Hosts file is consistent, and after the configuration file is modified,You need to restart windows serice to make the configuration take effect.
  


Finally, add the source code,
Reprinted please indicate the source, the invitation to the 3w@live.cn thank you!

Source code download

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.