偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

C#Windows服務(wù)程序開發(fā)實(shí)例淺析

開發(fā) 后端
C#Windows服務(wù)程序開發(fā)實(shí)例向你介紹一個(gè)定時(shí)從數(shù)據(jù)庫中拿出記錄發(fā)送郵件的程序執(zhí)行,希望對(duì)你了解和學(xué)習(xí)C#Windows服務(wù)程序開發(fā)有所幫助。

C#Windows服務(wù)程序開發(fā)實(shí)例:編寫一個(gè)C#Windows服務(wù)程序,定時(shí)從數(shù)據(jù)庫中拿出記錄發(fā)送郵件。

測(cè)試環(huán)境:Visual Studio 2005 SP1、Windows Server 2003 SP2

C#Windows服務(wù)程序開發(fā)實(shí)例一、新建項(xiàng)目

打開VS2005,新建一個(gè)“Windows 服務(wù)”項(xiàng)目。

C#Windows服務(wù)程序開發(fā)實(shí)例二、添加Timer

展開“工具箱”,在“組件”標(biāo)簽下找到“Timer”雙擊,這時(shí)就添加了一個(gè)Timer組件,修改“Name”屬性為“timEmail”、“Enabled”為“false”、“Interval”為“60000”。

接下來要做一些修補(bǔ)工作,不知是VS2005的BUG還是我沒找著地方,在VS2003下是不存在該問題的:剛從“組件”下添加的“Timer”按理說應(yīng)該來自“System.Timers命名空間”,也只有“System.Timers.Timer”才能在Windows服務(wù)程序中正常工作,但是現(xiàn)在這個(gè)Timer卻是屬于“System.Windows.Forms.Timer”的。所以得稍作修改,打開“.Designer.cs”文件,修改如下:

  1. #region 組件設(shè)計(jì)器生成的代碼  
  2. //........以上略  
  3. /// <summary>   
  4. /// 設(shè)計(jì)器支持所需的方法 - 不要  
  5. /// 使用代碼編輯器修改此方法的內(nèi)容。  
  6. /// </summary>  
  7. private void InitializeComponent()  
  8. {  
  9.     this.components = new System.ComponentModel.Container();  
  10.     //this.timEmail = new System.Windows.Forms.Timer(this.components);原  
  11.     this.timEmail = new System.Timers.Timer();//改  
  12.     this.timEmail.Interval = 60000;  
  13.     this.ServiceName = "Service1";  
  14. }  
  15. #endregion  
  16. //private System.Windows.Forms.Timer timEmail;原  
  17. private System.Timers.Timer timEmail;//改 

C#Windows服務(wù)程序開發(fā)實(shí)例三、添加配置文件

服務(wù)每次調(diào)用配置文件,獲取一些基本參數(shù),這樣一些變更就可直接修改配置文件而不必修改代碼。新建ServiceConfig.xml存放于項(xiàng)目“Bin\Debug\”下:

  1. ﹤?xml version="1.0" encoding="utf-8" ?﹥   
  2. ﹤serviceConfig﹥  
  3.     ﹤serviceItem   
  4. name="sendEmail"   
  5. enable="true"   
  6. elapsed="60000"   
  7. connectionString="your database connection..."   
  8. smtp="smtp address"   
  9. account="your email account..."   
  10. password="your password..." ﹥  
  11.     ﹤/serviceItem﹥  
  12. ﹤/serviceConfig﹥ 

C#Windows服務(wù)程序開發(fā)實(shí)例四、以下是實(shí)現(xiàn)代碼

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Diagnostics;  
  6. using System.ServiceProcess;  
  7. using System.Text;  
  8. using System.Xml;//操作配置文件  
  9. using System.IO;//寫日志  
  10. using System.Threading;//使用線程  
  11.  
  12. namespace ClientWindowsService  
  13. {  
  14.     public partial class ClientService : ServiceBase  
  15.     {  
  16. public ClientService()  
  17. {  
  18.     InitializeComponent();  
  19. }  
  20.  
  21. protected override void OnStart(string[] args)  
  22. {  
  23.     //服務(wù)啟動(dòng)  
  24.       this.timEmail.Enabled = true;  
  25.     this.tSendEmail();  
  26. }  
  27.  
  28. protected override void OnStop()  
  29. {  
  30.     //服務(wù)停止  
  31.       this.timEmail.Enabled = false;  
  32. }  
  33.  
  34. private void timEmail_Elapsed(object sender,  
  35.  System.Timers.ElapsedEventArgs e)  
  36. {  
  37.     //定時(shí)器  
  38.       this.tSendEmail();  
  39. }  
  40.  
  41. //開啟新進(jìn)程發(fā)送郵件  
  42.     private void tSendEmail()  
  43. {  
  44.     Thread t = new Thread(new ThreadStart(sendEmail));  
  45.     t.Start();  
  46. }  
  47.  
  48. //發(fā)送郵件函數(shù)  
  49.     private void sendEmail()  
  50. {  
  51.     XmlDocument doc = new XmlDocument();  
  52.     //添加System.Windows.Forms引用,獲取執(zhí)行目錄  
  53.       string configFile = System.Windows.Forms.Application.  
  54. StartupPath.ToString() + "\ServiceConfig.xml";  
  55.     doc.Load(@configFile);  
  56.     XmlElement root = doc.DocumentElement;  
  57.     foreach (XmlNode node in root)  
  58.     {  
  59. //如果配置文件中開啟服務(wù)  
  60. if (node.Attributes["name"].Value == "sendEmail" &&  
  61.  node.Attributes["enable"].Value == "true")  
  62. {  
  63.     try 
  64.     {  
  65. //讀取數(shù)據(jù)庫,發(fā)送郵件操作,略  
  66.     }  
  67.     catch (Exception error)  
  68.     {  
  69. //寫錯(cuò)誤日志  
  70.     using (StreamWriter sw = new 
  71. StreamWriter(System.Windows.Forms.
  72. Application.StartupPath.ToString() + 
  73. @"" + DateTime.Now.ToString("yyyy-MM-dd") + 
  74. ".txt"true, System.Text.Encoding.UTF8))  
  75. {  
  76.     sw.WriteLine(DateTime.Now.ToString() + ":");  
  77.     sw.WriteLine(error.ToString());  
  78.     sw.WriteLine("----------------
  79. -----------------------------");  
  80.     sw.Close();  
  81. }  
  82.     }  
  83. }  
  84.     }//end foreach  
  85. }  
  86.  
  87.     }//end class  
  88. }//end namespace 

C#Windows服務(wù)程序開發(fā)實(shí)例五、布署服務(wù)

在設(shè)計(jì)模式下右鍵-->添加安裝程序-->設(shè)置serviceProcessInstaller1的Account為LocalSystem

設(shè)置serviceInstaller1的StartType為Automatic

編譯

在命令模式下執(zhí)行:%systemroot%\microsoft.net\framework\v2.0.50727\installUtil.exe D:\項(xiàng)目目錄\bin\Debug\可執(zhí)行文件名.exe

在每次需要修改Windows服務(wù)時(shí),這就會(huì)要求你卸載和重新安裝這個(gè)服務(wù)。不過要注意在卸載這個(gè)服務(wù)前,最好確保服務(wù)管理控制臺(tái)已經(jīng)關(guān)閉,這會(huì)是一個(gè)很好的習(xí)慣。如果沒有這樣操作的話,你可能在卸載和重安裝Windows服務(wù)時(shí)會(huì)遇到麻煩。僅卸載服務(wù)的話,可以執(zhí)行相的InstallUtil命令用于注銷服務(wù),不過要在后面加一個(gè)/u命令開關(guān)。

調(diào)試Windows服務(wù)

從另外的角度度看,調(diào)試Windows服務(wù)絕不同于一個(gè)普通的應(yīng)用程序。調(diào)試Windows服務(wù)要求的步驟更多。服務(wù)不能象你對(duì)普通應(yīng)用程序做的那樣,只要簡(jiǎn)單地在開發(fā)環(huán)境下執(zhí)行就可以調(diào)試了。服務(wù)必須首先被安裝和啟動(dòng),這一點(diǎn)在前面部分我們已經(jīng)做到了。為了便于跟蹤調(diào)試代碼,一旦服務(wù)被啟動(dòng),你就要用Visual Studio把運(yùn)行的進(jìn)程附加進(jìn)來(attach)。記住,對(duì)你的Windows服務(wù)做的任何修改都要對(duì)這個(gè)服務(wù)進(jìn)行卸載和重安裝。

附加正在運(yùn)行的Windows服務(wù)

為了調(diào)試程序,有些附加Windows服務(wù)的操作說明。這些操作假定你已經(jīng)安裝了這個(gè)Windows服務(wù)并且它正在運(yùn)行。

1. 用Visual Studio裝載這個(gè)項(xiàng)目

2. 點(diǎn)擊“調(diào)試”菜單

3. 點(diǎn)擊“進(jìn)程”菜單

4. 確保 顯示系統(tǒng)進(jìn)程 被選

5. 在 可用進(jìn)程 列表中,把進(jìn)程定位于你的可執(zhí)行文件名稱上點(diǎn)擊選中它

6. 點(diǎn)擊 附加 按鈕

7. 點(diǎn)擊 確定

8. 點(diǎn)擊 關(guān)閉

9. 在timer1_Elapsed方法里設(shè)置一個(gè)斷點(diǎn),然后等它執(zhí)行

C#Windows服務(wù)程序開發(fā)實(shí)例六、代碼下載

http://files.cnblogs.com/linckle/log.rar

C#Windows服務(wù)程序開發(fā)實(shí)例的基本內(nèi)容就向你介紹到這里,希望對(duì)你學(xué)習(xí)和了解C#Windows服務(wù)程序開發(fā)實(shí)例有所幫助。

【編輯推薦】

  1. C#Windows服務(wù)介紹
  2. C#創(chuàng)建Windows服務(wù)程序淺析
  3. C#Windows服務(wù)之添加文件監(jiān)視服務(wù)
  4. C#創(chuàng)建Windows服務(wù)學(xué)習(xí)的一點(diǎn)體會(huì)
  5. C#Windows服務(wù)程序之添加安裝程序圖解
責(zé)任編輯:仲衡 來源: cnblogs
相關(guān)推薦

2009-08-14 14:25:09

Windows服務(wù)程序

2009-08-14 15:19:38

Windows服務(wù)程序Windows服務(wù)

2009-08-14 16:24:00

Windows服務(wù)程序

2009-08-14 15:47:18

C#Windows服務(wù)

2009-08-14 15:06:08

Windows服務(wù)程序

2009-08-14 15:54:50

Windows服務(wù)程序C#Windows服務(wù)

2009-08-14 14:45:03

C#Windows服務(wù)

2009-08-14 16:13:25

C#windows服務(wù)

2009-08-14 16:48:39

C#Windows服務(wù)

2009-08-14 10:50:09

Windows服務(wù)介紹

2009-08-14 13:41:13

C#Windows服務(wù)

2009-08-14 17:36:20

C#Windows應(yīng)用

2009-08-14 17:27:30

C#Windows應(yīng)用

2009-08-14 14:53:55

WINDOWS服務(wù)交互

2009-08-14 17:55:52

C#Windows應(yīng)用

2009-08-14 17:43:20

C#Windows應(yīng)用

2009-08-14 11:15:19

文件監(jiān)視C#Windows服務(wù)

2009-08-14 11:00:16

C#創(chuàng)建Windows

2009-08-14 18:00:22

C#Windows應(yīng)用

2009-08-14 18:04:59

C#Windows應(yīng)用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)