【发布时间】:2011-11-07 16:52:29
【问题描述】:
所以我在 VB.Net 中创建了这个 Windows 服务,它所做的只是执行几个调用并坐在那里。 (真的什么都不做)
我已将它安装在测试机器上,当我手动启动它时,它会启动。当我手动停止时,正确关闭(调用 onStop)。
当我启动它并重新启动机器时,服务会正常启动,但是当我登录时,服务不再运行,并且 onStop 从未显示为正在执行。
我以管理员权限安装了该服务,所以这应该不是问题。
这是我所拥有的:
Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'' Open our log file
FileReader = New IO.StreamWriter("C:\test.txt", True)
Log("Starting service...")
'' Get the username from the process list (If explorer is open (Meaning a user is logged in)
Dim ProcessList As System.Diagnostics.Process()
ProcessList = System.Diagnostics.Process.GetProcesses()
Dim Proc As System.Diagnostics.Process
For Each Proc In ProcessList
If Proc.ProcessName.ToLower() = "explorer" Then
My.Settings.Username = GetProcessUserName(Proc)
Exit For
End If
Next
Log("Fetched user name: " & My.Settings.Username)
Try
'' Get the IP address of the machine
Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
My.Settings.IP = h.AddressList.GetValue(0).ToString
'' Get the Machine Information
My.Settings.MachineName = System.Environment.MachineName()
'' Get the Operating System Version
Dim osInfo As New FindOsInfo
My.Settings.OS = osInfo.OsName.ToString().Replace(" ", "").Split("|")(0)
Catch ex As Exception
Log("Startup error: " & ex.Message)
End Try
'' Start server listen here
End Sub
'Protected Overrides Sub OnStart(ByVal args() As String)
'End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
Log("Closing down service...")
FileReader.Close()
End Sub
Public Sub Log(ByVal str As String)
Dim time As DateTime = DateTime.Now
FileReader.WriteLine("[" & time.ToString("G") & "] " & str)
FileReader.Flush()
End Sub
感谢您的宝贵时间!
【问题讨论】:
-
您是否在 Windows (services.msc) 中将服务设置为“自动”?
-
是的,它设置为自动。我很抱歉没有具体说明。