无法理解我的Windows服务仅在调试模式下工作的原因。
仅使用了三个功能。
<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
虽然它处于调试模式,但它作为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
但是当它在没有调试的情况下使用(已安装并运行)时,它不会弹出任何错误消息。状态正在运行。但没有任何反应。甚至没有日志消息“正在开始”......
编辑:在pastebin PASTEBIN.INIT
上的Init类答案 0 :(得分:0)
主要的问题是,当我把路径放到像这样的日志文件时,我应该在盒子外思考
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
它不起作用,因为我需要输入Service_Log.txt文件的完整路径。