我在VS 2010中创建了一个Windows服务。我安装它并同时运行它并将startup
类型设置为Automatic
。我看到它通过EventViewer
正常运行并且已成功完成。
但在那之后我看到EventViewer
显示任何内容,即使工作正在进行,它仍然应该检查数据库并跳过所有行完成。
那么问题是什么?
我是否需要在服务中使其成为无限循环才能使其保持运行?
像
这样的东西虽然(DB中的行数=空)?
因为它似乎不像任务调度程序那样工作!
答案 0 :(得分:1)
是的,你需要做一个循环,有可能再次打破它。示例服务(VB.NET):
Public Class MyService
Protected Property IsRunning As Boolean = False
Protected Sub OnStart(args() As String)
IsRunning = True
' make the loop function run asynchronously
Dim t As New System.Threading.Thread(AddressOf MyLoopFunction)
t.Start()
End Sub
Protected Sub MyLoopFunction
While IsRunning
' here comes your code ...
' sleep for a second for better CPU freedom
System.Threading.Thread.Sleep(1000)
End While
End Sub
Protected Sub OnStop()
IsRunning = False
End Sub
End Class