×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

Service1.vb

本文发表在 rolia.net 枫下论坛Imports System.ServiceProcess
Imports System.IO

Public Class Service1
Inherits System.ServiceProcess.ServiceBase

#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

' This call is required by the Component Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call

End Sub

'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
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 Region

<MTAThread()> _
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}
'
Dim obj1 As ServiceBase
Dim obj2 As ServiceBase

obj1 = New Service1
obj2 = New Service2

obj1.ServiceName = "Service1"
obj2.ServiceName = "Service2"

obj1.AutoLog = True
obj2.AutoLog = True
ServicesToRun = New System.ServiceProcess.ServiceBase() {obj1, obj2}

System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub

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.
File.Create("C:\service1").Close()

End Sub

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

End Class更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / .net问题:我有一个windows service project,其中有4个不同的services,生成不同的4个installer,安装后,启动任何一个windows service,却总是运行同一个service。请问如何解决。
    • 你不会service installer里面的名字一样吧?
      • 在initialcomponent里面,每个me.installer.servicename都是不一样的。这是你说的么?而且我是每个service分别add installer. 谢谢。
        • up
          • 一个windows services project 里面是不是不能存在几个services啊?
    • Try to create 4 different projects in a solution or just one project for each service, 另外想问,不同的Services为什么放在同一个Project里呢?
      • I might got the answer, just testing it now. Putting them in one project, so you can install them once, and of course, it looks a little bit professional. :P
    • 目前我试着这样做,不过好像还是总是运行第一个service。
      <MTAThread()>_
      shared sub main()
      dim servicesToRun as serviceBase
      servicesToRun=New ServiceBase(new service1, new service2, new service3, new service4)
      servicebase.run(servicesToRun)
      End sub
      • I tried this method too. But I did not make it. I don't know if i did something wrong.
      • dim servicesToRun as serviceBase 还是 dim servicesToRun() as serviceBase ?应该定义一个数组吧。试试:
        dim servicesToRun() as serviceBase
        servicesToRun=New ServiceBase()(new service1, new service2, new service3, new service4)
        • 我就是这样的,一模一样。可是我打开service2时,还是总是运行sub main 所在的service1.
          • From MSDN:
            You must set the service name of the service in the main() function of the executable. The service name that you set in main() must exactly match the ServiceName property of the service installer.

            The URL you gave above should be the correct answer, I will try it tomorrow, I have to go.
            • 谢谢你,做个好梦。
              • works for me here is the code.
                • Service1.vb
                  本文发表在 rolia.net 枫下论坛Imports System.ServiceProcess
                  Imports System.IO

                  Public Class Service1
                  Inherits System.ServiceProcess.ServiceBase

                  #Region " Component Designer generated code "

                  Public Sub New()
                  MyBase.New()

                  ' This call is required by the Component Designer.
                  InitializeComponent()

                  ' Add any initialization after the InitializeComponent() call

                  End Sub

                  'UserService overrides dispose to clean up the component list.
                  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
                  If disposing Then
                  If Not (components Is Nothing) Then
                  components.Dispose()
                  End If
                  End If
                  MyBase.Dispose(disposing)
                  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 Region

                  <MTAThread()> _
                  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}
                  '
                  Dim obj1 As ServiceBase
                  Dim obj2 As ServiceBase

                  obj1 = New Service1
                  obj2 = New Service2

                  obj1.ServiceName = "Service1"
                  obj2.ServiceName = "Service2"

                  obj1.AutoLog = True
                  obj2.AutoLog = True
                  ServicesToRun = New System.ServiceProcess.ServiceBase() {obj1, obj2}

                  System.ServiceProcess.ServiceBase.Run(ServicesToRun)
                  End Sub

                  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.
                  File.Create("C:\service1").Close()

                  End Sub

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

                  End Class更多精彩文章及讨论,请光临枫下论坛 rolia.net
                • Service2.vb
                  本文发表在 rolia.net 枫下论坛Imports System.ServiceProcess
                  Imports System.IO

                  Public Class Service2
                  Inherits System.ServiceProcess.ServiceBase

                  #Region " Component Designer generated code "

                  Public Sub New()
                  MyBase.New()

                  ' This call is required by the Component Designer.
                  InitializeComponent()

                  'Add any initialization after the InitializeComponent() call

                  End Sub

                  'UserService overrides dispose to clean up the component list.
                  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
                  If disposing Then
                  If Not (components Is Nothing) Then
                  components.Dispose()
                  End If
                  End If
                  MyBase.Dispose(disposing)
                  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 = "Service2"
                  End Sub

                  #End Region

                  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.
                  File.Create("C:\service2").Close()

                  End Sub

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

                  End Class更多精彩文章及讨论,请光临枫下论坛 rolia.net
                • ProjectInstaller.vb
                  本文发表在 rolia.net 枫下论坛Imports System.ComponentModel
                  Imports System.Configuration.Install

                  <RunInstaller(True)> Public Class ProjectInstaller
                  Inherits System.Configuration.Install.Installer

                  #Region " Component Designer generated code "

                  Public Sub New()
                  MyBase.New()

                  'This call is required by the Component Designer.
                  InitializeComponent()

                  'Add any initialization after the InitializeComponent() call

                  End Sub

                  'Installer overrides dispose to clean up the component list.
                  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
                  If disposing Then
                  If Not (components Is Nothing) Then
                  components.Dispose()
                  End If
                  End If
                  MyBase.Dispose(disposing)
                  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.
                  Friend WithEvents ServiceProcessInstaller1 As System.ServiceProcess.ServiceProcessInstaller
                  Friend WithEvents ServiceInstaller1 As System.ServiceProcess.ServiceInstaller
                  Friend WithEvents ServiceInstaller2 As System.ServiceProcess.ServiceInstaller
                  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
                  Me.ServiceProcessInstaller1 = New System.ServiceProcess.ServiceProcessInstaller
                  Me.ServiceInstaller1 = New System.ServiceProcess.ServiceInstaller
                  Me.ServiceInstaller2 = New System.ServiceProcess.ServiceInstaller
                  '
                  'ServiceProcessInstaller1
                  '
                  Me.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem
                  Me.ServiceProcessInstaller1.Password = Nothing
                  Me.ServiceProcessInstaller1.Username = Nothing
                  '
                  'ServiceInstaller1
                  '
                  Me.ServiceInstaller1.ServiceName = "Service1"
                  '
                  'ServiceInstaller2
                  '
                  Me.ServiceInstaller2.ServiceName = "Service2"
                  '
                  'ProjectInstaller
                  '
                  Me.Installers.AddRange(New System.Configuration.Install.Installer() {Me.ServiceProcessInstaller1, Me.ServiceInstaller1, Me.ServiceInstaller2})

                  End Sub

                  #End Region

                  End Class更多精彩文章及讨论,请光临枫下论坛 rolia.net