我想使用VB.Net創建Windows服務,但在VS2008 Standard中,您似乎無法獲得「Windows服務「之前使用過的模板。如何在Studio 2008 Standard中使用vb.net創建Windows服務(無模板)
創建此類服務的最佳方式是什麼,而不是訴諸於使用C++模板?
感謝
我想使用VB.Net創建Windows服務,但在VS2008 Standard中,您似乎無法獲得「Windows服務「之前使用過的模板。如何在Studio 2008 Standard中使用vb.net創建Windows服務(無模板)
創建此類服務的最佳方式是什麼,而不是訴諸於使用C++模板?
感謝
Windows服務模板沒什麼特別的。它會在你的項目中的兩個類文件,並增加了System.ServiceProcess
參考:
Service1.vb:
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.
End Sub
Protected Overrides Sub OnStop()
'' Add code here to perform any tear-down necessary to stop your service.
End Sub
End Class
Service1.Designer.vb:
Imports System.ServiceProcess
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Service1
Inherits System.ServiceProcess.ServiceBase
''UserService overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'' The main entry point for the process
<MTAThread()> _
<System.Diagnostics.DebuggerNonUserCode()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
'' More than one NT Service may run within the same process. To add
'' another service to this process, change the following line to
'' create a second service object. For example,
''
'' ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1, New MySecondUserService}
''
ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub
''Required by the Component Designer
Private components As System.ComponentModel.IContainer
'' NOTE: The following procedure is required by the Component Designer
'' It can be modified using the Component Designer.
'' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
Me.ServiceName = "Service1"
End Sub
End Class
創建從了System.ServiceProcess.ServiceBase繼承的類,覆蓋的OnStart和調用OnStop。使用installutil在您的機器上註冊服務。
真棒,謝謝:) – Cylindric 2009-12-09 09:31:55