C# 寫windows服務

來源:互聯網
上載者:User


因為需要每天定時更新資料,所以決定寫成一個windos 服務。

1。在VS2008建立windows service工程

檔案---建立---項目----windows服務,名稱AdioService

2。結構如所示,雙擊service1.cs,在onstart中寫具體代碼,注意如果代碼執行需要很長時間,則需要將方法放在子線程中,否則windows服務會啟動不起來。

3。定時器Quartz的使用

 

4。添加安裝程式

在AdioService.cs[設計]右鍵---添加安裝程式,會出現

注意:account選擇localhost  ;StartType選擇Automatic(開機自動執行)

5。安裝服務

開始-運行-cmd
cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

找到debug下的.exe,複製路徑(例如 C:\Documents and Settings\Administrator\案頭\Audiomonitor\d1\AudioService\bin\Debug\AudioService.exe")

安裝命令

InstallUtil  "C:\Documents and Settings\Administrator\案頭\Audiomonitor\d\AudioService\bin\Debug\AudioService.exe"

卸載命令

InstallUtil  /u "C:\Documents and Settings\Administrator\案頭\Audiomonitor\d1\AudioService\bin\Debug\AudioService.exe"

在控制台---管理工具-----服務----右鍵----啟動服務

6。異常情況

如果不能正常啟動服務,說明服務有錯誤。可以利用“日誌查看器”查看錯誤資訊。(調試比較麻煩)。

注意每次對服務改動的話,都必須重新安裝。

7。想要服務在遇到異常停止後自動重啟

找到  服務-----右鍵----屬性---恢複-----重新啟動服務

 

定時程式請關注我的一下篇!O(∩_∩)O~  (今天總算買到票了,有時間寫點東東了)

相關文章

聯繫我們

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