使用计时器和System.ServiceProcess观察Windows服务状态时内存泄漏

时间:2017-09-15 01:27:16

标签: .net vb.net winforms windows-services

我使用计时器和System.ServiceProcess命名空间来检查Windows服务的状态。我成功地做了,但应用程序的内存使用率不断上升。这是我的代码,我在这里缺少什么?服务控制器已经在使用块中。

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    servicetimer.AutoReset = True
    servicetimer.Interval = 1
    AddHandler servicetimer.Elapsed, AddressOf tick
    servicetimer.Enabled = True
    servicetimer.Start()
End Sub

Private Sub tick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
    Using sc As New ServiceController("Smart Card")
        If sc.Status = ServiceControllerStatus.Stopped Or sc.Status = ServiceControllerStatus.StopPending Then
            'Do stuff when stopped
        ElseIf sc.Status = ServiceControllerStatus.Running Or sc.Status = ServiceControllerStatus.StartPending Then
            'Do Stuff when started
        Else

        End If
    End Using

End Sub

1 个答案:

答案 0 :(得分:0)

好的,这有点令人尴尬。没有内存泄漏,当我启动应用程序时,内存使用量持续上升,我认为是内存泄漏,所以我试图阻止它发生,原来这是正常的,并在一段时间后停止增长。