如何修复内存泄漏多线程

时间:2019-05-01 13:57:56

标签: vb.net multithreading

我有一个应用,我希望datagridview刷新每行5秒。我的代码运行良好,除非我发生内存泄漏,我想所有资源都没有释放...但是它是一个循环运行线程,我不想处理它...

我这样开始我的任务

Private Sub Desktop_Load(sender As Object, e As EventArgs) Handles Me.Load

    Dim F As String = Main.UserDataFolder & "\Wemine4U\Config\MinerList.wemine"
    If IO.File.Exists(F) = True Then
        Dim NOF2 As Integer = FreeFile()
        Dim texte2 As String
        ListView1.Items.Clear()
        FileOpen(NOF2, Main.UserDataFolder & "\Wemine4U\Config\MinerList.wemine", OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
        Do Until EOF(NOF2)
            texte2 = LineInput(NOF2)
            Main.DataMiner.Rows.Add(Split(texte2, Chr(9))(1).ToString, "0" + " C", "0.00", "0.00", "0", "0", "0", "0", "N/D", "N/D", Split(texte2, Chr(9))(3).ToString, "", "", "0")

            Dim updatem As System.Threading.Thread
            updatem = New Thread(AddressOf loadTableMiner)
            updatem.Name = Split(texte2, Chr(9))(1) & Chr(9) & Split(texte2, Chr(9))(3)
            updatem.IsBackground = True
            updatem.Start()

        Loop
        FileClose(NOF2)
    End If
    DataGridView1.DataSource = DataSource
    DataGridView1.Columns(11).Visible = False
    DataGridView1.Columns(12).Visible = False
    DataGridView1.Columns(13).Visible = False


End Sub

在我想要循环运行这样的线程之后

Private Sub loadTableMiner()

StartLoadTable:

    Dim ip As String = Split(Thread.CurrentThread.Name, Chr(9))(0)
        Dim clientssh As SshClient
        Dim cmd As SshCommand
        Dim tview As String
        Dim ValueSSH As String
        Dim ValueSSHsummary As String
        Dim Accepteds As String
        Dim Rejects As String
        Dim Hwerror As String
        Dim Discarded As String
        Dim ValueTemp As String
        Dim IndexOfTable As Integer
        Dim Type As String = Split(Thread.CurrentThread.Name, Chr(9))(1)
        Dim MS As String
        Dim GH As String
        Dim GHS As String
        Dim ValueGHSDIFF As Decimal
        Dim ValueGHSM
        Dim NBV As Integer
        Dim nbvt As Integer
        Dim TT As String
        Dim NBI As Integer
        Dim ValueGHS
        Dim ValueReboot As Decimal

RestartF:


    Try
        If clientssh Is Nothing Then
            clientssh = New SshClient(ip, "root", "admin")
            clientssh.Connect()
        End If

        If Type = "AntMiner S9" Then
            cmd = clientssh.RunCommand("bmminer-api -o stats")
        End If
        If Type = "AntMiner L3+" Then
            cmd = clientssh.RunCommand("cgminer-api -o stats")
        End If

        ValueSSH = cmd.Result

        If Type = "AntMiner S9" Then
            cmd = clientssh.RunCommand("bmminer-api -o summary")
        End If
        If Type = "AntMiner L3+" Then
            cmd = clientssh.RunCommand("cgminer-api -o summary")
        End If

        ValueSSHsummary = cmd.Result
    Catch ex As Exception
        clientssh.Disconnect()
        clientssh = Nothing

        Thread.CurrentThread.Sleep(5000)

        GoTo RestartF

    End Try

        If InStr(ValueSSH, "Connection refused") > 0 Or InStr(ValueSSHsummary, "Connection refused") > 0 Then
            PutDataInTable(IndexOfTable, ip, "0", "0.00", "0.00", "0", "0", "0", "0", "N/D", "Connection refused", Type, "", "", "0")
            clientssh.Disconnect()
        clientssh = Nothing
        Thread.CurrentThread.Sleep(5000)

        GoTo RestartF
        End If

        If ValueSSH = "" Or ValueSSHsummary = "" Then
            PutDataInTable(IndexOfTable, ip, "0", "0.00", "0.00", "0", "0", "0", "0", "N/D", "Not Online", Type, "", "", "0")
            clientssh.Disconnect()
        clientssh = Nothing

        Thread.CurrentThread.Sleep(5000)

        GoTo RestartF
        Else

            Accepteds = Split(Split(ValueSSHsummary, "Accepted=")(1).ToString, ",")(0)
            Rejects = Split(Split(ValueSSHsummary, "Rejected=")(1).ToString, ",")(0)
            Hwerror = Split(Split(ValueSSHsummary, "Hardware Errors=")(1).ToString, ",")(0)
            Discarded = Split(Split(ValueSSHsummary, "Discarded=")(1).ToString, ",")(0)

            ValueTemp = Split(Split(ValueSSH, ",")(33).ToString, "=")(1)
            NBV = CountCharacter(ValueSSH, ",")
            nbvt = 0
            Do Until nbvt = NBV
                TT = Split(ValueSSH, ",")(nbvt).ToString
                NBI = InStr(TT, "temp")
                If NBI > 0 Then
                    tview = Split(Split(ValueSSH, ",")(nbvt), "=")(1)
                    If CInt(tview) > CInt(ValueTemp) Then
                        ValueTemp = Split(Split(ValueSSH, ",")(nbvt).ToString, "=")(1)
                    End If

                End If
                nbvt = nbvt + 1
            Loop

            If 20 > ValueTemp Or ValueTemp > 100 Then
                SSHReboot(ip)
                PutDataInTable(IndexOfTable, ip, "0", "0.00", "0.00", "0", "0", "0", "0", "N/D", "Rebooting Temp", Type, "", "", "0")
                clientssh.Disconnect()
            clientssh = Nothing

            Thread.CurrentThread.Sleep(5000)

            GoTo RestartF

            End If

            If Language = "fr-CA" Then
                If Type = "AntMiner S9" Then
                    ValueGHS = CDec(Replace(Split(Split(ValueSSH, ",")(14).ToString, "=")(1), ".", ",")) / 1000
                End If
                If Type = "AntMiner L3+" Then
                    ValueGHS = CDec(Replace(Split(Split(ValueSSH, ",")(14).ToString, "=")(1), ".", ","))
                End If
            Else
                If Type = "AntMiner S9" Then
                    ValueGHS = CDec(Replace(Split(Split(ValueSSH, ",")(14).ToString, "=")(1), ".", ".")) / 1000
                End If
                If Type = "AntMiner L3+" Then
                    ValueGHS = CDec(Replace(Split(Split(ValueSSH, ",")(14).ToString, "=")(1), ".", "."))
                End If
            End If


            If Language = "fr-CA" Then
                If Type = "AntMiner S9" Then
                    ValueGHSM = CDec(Replace(Split(Split(ValueSSH, ",")(15).ToString, "=")(1), ".", ",")) / 1000
                End If
                If Type = "AntMiner L3+" Then
                    ValueGHSM = CDec(Replace(Split(Split(ValueSSH, ",")(15).ToString, "=")(1), ".", ","))
                End If
            Else
                If Type = "AntMiner S9" Then
                    ValueGHSM = CDec(Replace(Split(Split(ValueSSH, ",")(15).ToString, "=")(1), ".", ".")) / 1000
                End If
                If Type = "AntMiner L3+" Then
                    ValueGHSM = CDec(Replace(Split(Split(ValueSSH, ",")(15).ToString, "=")(1), ".", "."))
                End If
            End If

            If ValueGHS > 0 Then
                MS = "Mining"
            Else
                MS = "Problems"
            End If
            ValueGHSDIFF = ValueGHSM - ValueGHS

            GH = Format(ValueGHS, "0.00")
            GHS = Format(ValueGHSM, "0.00")
            If Language = "fr-CA" Then
                ValueReboot = "0,5"
            Else
                ValueReboot = "0.5"
            End If
            If ValueGHSDIFF > ValueReboot Then
                SSHReboot(ip)
                PutDataInTable(IndexOfTable, ip, "0", "0.00", "0.00", "0", "0", "0", "0", "N/D", "Rebooting GH Low", Type, "", "", "0")
                clientssh.Disconnect()
            clientssh = Nothing

            Thread.CurrentThread.Sleep(5000)

            GoTo RestartF
            End If
        End If


repprogress:

        PutDataInTable(IndexOfTable, ip, ValueTemp, GH, GHS, Accepteds, Rejects, Discarded, Hwerror, "N/D", MS, Type, ValueSSH, ValueSSHsummary, "0")

nextminer:


    Thread.CurrentThread.Sleep(5000)

    cmd = Nothing
        tview = ""
        ValueSSH = ""
        ValueSSHsummary = ""
        Accepteds = ""
        Rejects = ""
        Hwerror = ""
        Discarded = ""
        ValueTemp = ""
        IndexOfTable = 0
        MS = ""
        GH = ""
        GHS = ""
        ValueGHSDIFF = 0
        ValueGHSM = ""
        NBV = 0
        nbvt = 0
        TT = ""
        NBI = 0


    GoTo RestartF


Private Sub PutDataInTable(ByVal LineRendu As Integer, ByVal Ip As String, ByVal Temp As String, ByVal Gh As String, ByVal GHA As String, ByVal Accept As String, ByVal reject As String, ByVal discard As String, ByVal HWE As String, ByVal ps As String, ByVal ms As String, ByVal Type As String, ByVal lastvaluessh As String, ByVal lastvaluesshsummary As String, ByVal howmanystate As String)

    SyncLock DataGridView1

        Dim NBRow As Integer = DataGridView1.RowCount
        Dim RowRendu As Integer = 0
        Dim IndexTable As Integer

        Do Until RowRendu > NBRow
            If DataGridView1.Rows(RowRendu).Cells(0).Value = Ip Then
                GoTo EndSearchIndex
            End If
            RowRendu = RowRendu + 1
        Loop
EndSearchIndex:

        DataGridView1.Rows(RowRendu).Cells(1).Value = Temp & " C"
        DataGridView1.Rows(RowRendu).Cells(2).Value = Gh
        DataGridView1.Rows(RowRendu).Cells(3).Value = GHA
        DataGridView1.Rows(RowRendu).Cells(4).Value = Accept
        DataGridView1.Rows(RowRendu).Cells(5).Value = reject
        DataGridView1.Rows(RowRendu).Cells(6).Value = discard
        DataGridView1.Rows(RowRendu).Cells(7).Value = HWE
        DataGridView1.Rows(RowRendu).Cells(9).Value = ms
        DataGridView1.Rows(RowRendu).Cells(10).Value = Type
        DataGridView1.Rows(RowRendu).Cells(11).Value = lastvaluessh
        DataGridView1.Rows(RowRendu).Cells(12).Value = lastvaluesshsummary
        DataGridView1.Rows(RowRendu).Cells(13).Value = 0

    End SyncLock

并且内存慢慢长大...我不知道为什么...有人知道我的代码有什么问题吗?

0 个答案:

没有答案