本文介绍了首次运行后 Windows 服务未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 VS 2010 中创建了一个 Windows 服务.我安装它并同时运行它并将 startup 类型设置为 Automatic .我看到它通过 EventViewer 运行良好并成功完成.

I have create a windows service in VS 2010. I install it and also run it at the same time and set startup type to Automatic . I see it running fine through EventViewer and is successfully completed.

但在那之后我看到 EventViewer 显示任何东西,即使工作完成它仍然应该检查 DB 并在所有行完成时跳过.

But after that i done see EventViewer showing anything, even if the work is doen it still should check DB and skip as all rows done.

那么问题是什么?

我需要让它在服务中无限循环才能保持运行吗?

DO i need to make it an infinite loop in the service to keep it running?

类似的东西

虽然(数据库中的行!= null)?

While (ROWs in DB ! = null) ?

因为它似乎不像任务调度程序那样工作!

Because it does not seem it is working like task scheduler!

推荐答案

是的,您需要执行一个循环,并有可能再次打破它.示例服务(VB.NET):

Yes, you need to do a loop with the possibility to break it again. Example service (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

这篇关于首次运行后 Windows 服务未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 11:51