如何使用Windows Service?

來源:互聯網
上載者:User

(1)      在建立windowsservice程式時,Debug/Release的類型選擇為AnyCPU(特別是64位的機器)

(2)      添加安裝程式時,會產生兩個組件,比如ServiceProcessInstaller1,ServiceInstaller1

(3)      在為service添加安裝程式時,安裝程式(比如ServiceInstaller1)的Name屬性和Service的Name屬性要一致

(4)      ServiceProcessInstaller1的Account屬性必須設定為LocalSystem

(5)      安裝服務時,以管理員身份運行Viusial Studio命令列工具進行安裝。

安裝服務的命令: 

Installutil “程式地址”

(比如:Installutil "D:/Users/SPang/Documents/VisualStudio2010/Projects/WindowsService1/WindowsService1/bin/Debug/WindowsService1.exe")

卸載服務的命令:

Installutil “程式地址” /u

(比如:Installutil "D:/Users/SPang/Documents/VisualStudio2010/Projects/WindowsService1/WindowsService1/bin/Debug/WindowsService1.exe"/u )

(6)      安裝完成後啟動服務,可以通過我的MyComputer -> Manage -> System Tools -> EventViewer -> Windows Logs-> Application(我的電腦->管理->事件檢視器->windows日誌->應用程式)查看服務的日誌。

(7)      在使用命令時可能出現的錯誤: 

          i. an attempt was made to load a program withan incorrect format。解決方案:步驟1

          ii. the install failed, and the roll back hasbeen performed。解決方案:步驟4

最簡單的一個Windows Service程式碼範例(VB.NET):

C#代碼和這個差不多

Public Class Service1

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
EventLog.WriteEntry("Start Service")
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
End Sub
End Class

 

    相關文章

    聯繫我們

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