Service work in debug mode only

2019-08-30 06:47发布

Can't figure why my windows service work only in debug mode.

Only three functions used.

<System.Diagnostics.DebuggerNonUserCode()>
Shared Sub Main()

    'Dim servicio As New Service1
    'servicio.OnStart(Nothing)
    'System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)

    WriteLog("Starting it")
    Dim ServicesToRun() As System.ServiceProcess.ServiceBase
    ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}
    System.ServiceProcess.ServiceBase.Run(ServicesToRun)

End Sub

Public Sub MainWorker()
    While 1 = 1
        Try
            Dim mLoop As New Init
        Catch ex As Exception

        End Try
    End While
End Sub

While its on debug mode it goes as a charm on method OnStart.

  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.

        Try
            Dim T As New Thread(AddressOf MainWorker)
            T.Start()
        Catch ex As Exception

        End Try

    End Sub

But when its used without debug ( Installed and run ) it doesn't popup any error message. Status is running. But nothing happens. Not even the log message "Starting it" ...

Edit: Init class on pastebin PASTEBIN.INIT

1条回答
我只想做你的唯一
2楼-- · 2019-08-30 07:18

The main problem is i should think outside the box for some reason when i put path to log file like this

Public Sub WriteLog2(sLine As String)
        Dim strFile As String = "Service_Log.txt"
        Dim fileExists As Boolean = File.Exists(strFile)
        Using sw As New StreamWriter(File.Open(strFile, FileMode.Append))
            sw.WriteLine(sLine)
        End Using

        Console.WriteLine(sLine)
    End Sub

It does not work because i need to enter the full path to Service_Log.txt file.

查看更多
登录 后发表回答