【问题标题】:Memory leak when watching a windows service status with a timer and System.ServiceProcess使用计时器和 System.ServiceProcess 观察 Windows 服务状态时的内存泄漏
【发布时间】:2017-09-15 01:27:16
【问题描述】:

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

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

【问题讨论】:

  • 您确定泄漏不在代码的“做事”部分吗? (换句话说,您是否用 just 上面显示的代码重现了问题?)
  • @HarryJohnston "do stuff" 部分目前只是设置标签文本以显示服务的当前状态,我已将其删除以检查并且泄漏仍然发生。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多