So I have this Windows service created in VB.Net, and all it does is perform a few calls and sits there. (Does nothing really)
I have it installed on a test machine and when I start it manually, it starts up. When I stop manually, shuts down properly (onStop is called).
When I start it, and reboot the machine, the service starts like it should, but when I log in, the service isn't running anymore and the onStop never showed up as being executed.
I installed the service with admin privileges, so that shouldn't be an issue.
Here is what I have:
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
Thanks for your time!