在vb.net中创建Windows服务(服务在启动时停留)

时间:2014-07-01 04:07:37

标签: vb.net window

我正在尝试创建一个窗口。 安装服务后,服务状态停留在"开始"而不是开始或跑步。

    Protected Overrides Sub OnStart(ByVal args() As String)

        LoadSettings()

    Catch ex As Exception
        EventLog1.WriteEntry(ex.Message)
    End Try
End Sub

LoadSettings()方法,每隔几秒钟从数据库中抽取数据或从中抽取数据。

Private Sub LoadSettings()
        Do While 1 = 1
            Dim tStart As New Threading.Thread(AddressOf PumpData)
            tStart.Start()
            tStart.Join(1200000)
            Threading.Thread.Sleep(1000)
        Loop
End Sub

PumpData方法是一些sql查询。

1 个答案:

答案 0 :(得分:0)

请遵循以下代码:

Dim delPeriodicalCheck As New TimerCallback(AddressOf PumpData)
Dim objTimer As System.Threading.Timer
Dim dueTime = 1000 '1 Sec, delay start
Dim period = 60000 '1 Min, you may change this value

Protected Overrides Sub OnStart(ByVal args() As String)
objTimer = New System.Threading.Timer(delPeriodicalCheck, Nothing, dueTime,period)
Catch ex As Exception
EventLog1.WriteEntry(ex.Message)
End Try


Private Sub PumpData(ByVal stateInfo As Object)
'Your code
End Sub