C#製作Windows service服務系列二:示範一個定期執行的windows服務及調試(windows service)

來源:互聯網
上載者:User

  系列一: 製作一個可安裝、可啟動、可停止、可卸載的Windows service(downmoon原創)

  系列二:示範一個定期執行的windows服務及調試(windows service)(downmoon)

  系列三: windows service系列三--製作可控制介面的windows service

 

   一、經常有人問起如何讓程式定期自動執行
  除了像系統任務和SQL JOB/DTS等都可以滿足不同的使用者需求外,這裡示範了如何做一個簡單的windows serivce的架構。主要的功能是按照預先設定的時間間隔來執行類似的操作,比如抓取網頁,抓取天氣預報,群發郵件等。

  假定使用者有如下代碼需要定期自動執行:

Code
 1 擷取內容#region 擷取內容
 2        /**//// <summary>
 3        /// 擷取內容,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                    //此處從資料表中讀取資料,做一些事情,比如發郵件,抓取網頁內容等。
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        }

  那麼,只需要將以上代碼放在windows service環境下,部署成windows service,即可定期執行
  詳細過程我就省略了! 看文後源碼
  說明:
  1、SetupServer.bat ,雙擊可安裝服務
  2、unInstallServer.bat,雙擊可卸載服務
  3、主要檔案WSDemoByTime.exe
  4、設定檔WSDemoByTime.exe.config(主要配置數量、間隔時間、資料連線等)
  運行效果,這裡只示範了產生一個基本的記錄檔案。見圖;
  

  
  

  二、如何調試windows服務(windows service)。

  這裡提供簡單的處理過程:

  假定,我們已經部署好了windows service,結果發現不能如我們想像的運行,除了像本系列所描述的記錄日誌外,還可以直接調試進程。

  具體步驟見:

   一、調試--附加到進程

  
  

  二、設定斷點,調試
  
  三、如何修改windows service配置並生效?

      請注意,windows service的設定檔一般形如XXX.exe.Config,在本文中是WSDemoByTime.exe.Config,這個設定檔必須與WSDemoByTime.exe檔案一致,並且修改設定檔後,需要重新啟動windows serice才能使配置生效。
  


  最後,附上源碼,
轉載請註明出處,邀月 3w@live.cn 謝謝!

  源碼下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.