VB.NET和Delgates错误的想法

时间:2017-03-08 14:53:04

标签: vb.net datagridview delegates thread-safety

我通过我的comport发送和接收状态编号,工作正常。如果我发送一个新状态,我实现了一个datagridview,这也像我想要的那样工作,所以算法应该没问题。

另一种方式我也收到statusnumbers,在我的数据库中编写新的,然后想要实现datagridview。这是问题所在。

com-port-receive方法位于自己的模块中。它调用一个子“Empfangen”,它位于一个表单类中。这个子Empfangen()提取了重要的状态数据和其他信息,经过测试并且令人满意。之后我调用了一个子StatusAnzeigen_DataGridViewMA(),它将datagridview中的颜色更改为新状态。这里一定是问题,datagridview.rowcount告诉我1行,我在这里有4行,

所以我的想法是,使用委托。

我宣布了以下代表:

Public Delegate Sub StatusAnzeigen_DataGridViewMADelegate(ByVal msg As String)

这里是变色代码的一部分(如果发送状态,请记住它有效)

Private Sub StatusAnzeigen_DataGridViewMA()
On Error Resume Next
' On Error GoTo StatusAnzeigen_DataGridViewMA_ERR
Dim treeIcon As Icon
Dim Anzahl As Integer
Dim count As Integer
Dim Farbe As String
Dim cell As DataGridViewImageCell


If DataGridViewMA.InvokeRequired Then
DataGridViewMA.BeginInvoke(New StatusAnzeigen_DataGridViewMADelegate(AddressOf StatusAnzeigen_DataGridViewMA), New Object())

Return
End If
Anzahl = DataGridMA.RowCount
For count = 0 To Anzahl - 1

它永远不会使用invokerequired = true。

我有2个问题,这个想法是正确的吗?线程是问题吗? 如果是,我的错误在哪里,可能实施的地方是错的?

我第一次尝试这个,我真的很感兴趣。

***编辑因为洞图片的问题:

这是我的COM接收程序:

Public Sub MSCom_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles MSCom.DataReceived
    Dim buffer As Object
    Dim count As Integer
    buffer = ""
    count = 1
    Do
        Try
            buffer = CStr(MSCom.ReadLine)           ' Receive the data 
            Debug.Print("buffer: " & buffer)
            If (InStrRev(buffer, ": 13")) > 0 Then
                message = message + buffer
                buffer = ""
            End If
            If Len(message) > 0 And InStrRev(buffer, "84") Then
                buffer = message + buffer
                message = ""
            End If

        Catch ex As Exception
            buffer = ex.Message        ' Exception handling 
            Exit Sub
        End Try
    Loop Until (InStrRev(buffer, ": 13") > 0 Or InStrRev(buffer, ": 12") > 0 Or InStrRev(buffer, "AFF") > 0)
    If (InStrRev(buffer, ": 13") > 0 Or InStrRev(buffer, ": 12") > 0 Or InStrRev(buffer, "AFF") > 0) Then

        Form_Dashboard.Empfangen(buffer)
    End If
End Sub

以下代码是我的子Empfangen,它接收收到的字符串中的数字,状态等。

    Public Sub Empfangen(ByVal msg As String)
    On Error Resume Next

    Dim pos As Integer
    Dim pos1 As Integer
    Dim anrufer As String           'Rufnummer des Anrufers
    Dim anruferdez As Integer
    Dim hexStatus As String
    Dim decstatus As Integer
    Dim Statusstring As String
    Dim strT As String
    Dim typ As Integer
    Dim test As String
    Dim test1 As String
    strInput = ""

    If Len(msg) > 1 Then

        pos = InStrRev(strInput, db.eigeneRNR)
        pos1 = InStrRev(strInput, ": 13")
        test = Mid(strInput, 14, 7)       'Rufnummer sender
        test1 = Mid(strInput, pos + 10, 4)      'Status
        '-----------Status Eingang einfach-------------------------------------------
        If (pos > 0 And pos1 > 0) Then
            ' Anrufer und hexstatus aus strInput herauskopieren
            anrufer = Mid(strInput, 13, 7) ' 6 Stellen der Teilnehmernummer des Anrufers
            hexStatus = "&H" & Mid(msg, 36, Len(msg)) ' Status als Hexzahl
            If Microsoft.VisualBasic.Strings.Right(hexStatus, 3) <> "AFF" Then ' Wenn Adressat nicht ausgeschaltet 4AFF oder 5AFF
                ' 
                hexStatus = Microsoft.VisualBasic.Strings.Right(hexStatus, 5)
                hexStatus = Microsoft.VisualBasic.Strings.Left(hexStatus, 4)
                decstatus = CLng("&H" & hexStatus)   ' umwandeln in Dezimalzahl (Integer)
                decstatus = db.FindeTNStatusNr(decstatus)
                anruferdez = db.FindeTNPK(anrufer)
                db.Schreibe_StatusTN(anruferdez, decstatus, 1)     'Prüfen, ob Decstatus integer ist


                StatusAnzeigen_DataGridViewMA()

            Else
                decstatus = 98      'TN ausgeschaltet
                db.Schreibe_StatusTN(anrufer, decstatus, 1)
                StatusAnzeigen_DataGridViewMA()
            End If
        End If
        pos1 = InStrRev(strInput, ": 12")
        '-----------Status Eingang Quittung-------------------------------------------
        If (pos > 0 And pos1 > 0) Then
            'hexStatus = Microsoft.VisualBasic.Strings.Right(hexStatus, 5)
            'hexStatus = Microsoft.VisualBasic.Strings.Left(hexStatus, 4)
            anrufer = Mid(strInput, 13, 7) ' 6 Stellen der Teilnehmernummer des Anrufers
            If Microsoft.VisualBasic.Strings.Right(msg, 3) <> "AFF" Then ' Wenn Adressat nicht ausgeschaltet 4AFF oder 5AFF
                ' Anrufer und hexstatus aus strInput herauskopieren
                decstatus = 99
                db.Schreibe_StatusTN(anrufer, decstatus, 1)     'Prüfen, ob Decstatus integer ist
            Else
                decstatus = 98  ''TN ausgeschaltet
                db.Schreibe_StatusTN(anrufer, decstatus, 1)

            End If
            StatusAnzeigen_DataGridViewMA()
        End If
        strInput = ""


    End If
End Sub

这是所有功能验证,状态在数据库中更改。只有datagridview没有实现。

0 个答案:

没有答案