【问题标题】:DatagridView VB.Net, thread safetyDatagridView VB.Net,线程安全
【发布时间】:2015-10-02 19:52:18
【问题描述】:

所以我有这个 DataTable 我必须在 datagridview 中显示。 为此,我曾经在 form_load 中执行以下行

Me.Datagridview1.datasource = DT

如果我必须添加几行,它会起作用,但是当我添加足够多以使 datagridview 中的滚动条出现时,它会冻结程序。没有抛出异常,但我的程序会冻结,我认为这是因为我正在学习如何使用线程。

在这里,我开始监听另一个将发送字符串的程序,我必须将它们拆分并将它们放入数据表中的行中,然后显示它们。

Private Vstm As Stream
Dim tcp As New TcpClient
Dim DT As New DataTable("Acciones")
Dim Inter As New Interprete

Private Sub Conectar_Click(sender As Object, e As EventArgs) Handles Button1.Click
    tcp.Connect("192.168.1.143", 8050)
    Vstm = tcp.GetStream()
    Dim VtcpThd As New Thread(AddressOf LeerSocket)
    'Se arranca el subproceso.
    VtcpThd.Start()
End Sub

在 LeerSocket 的这里读不出来

Private Sub LeerSocket()
        Dim VbufferDeLectura() As Byte
        VbufferDeLectura = New Byte(100) {}
        While True
            'Se queda esperando a que llegue algún mensaje.
            Vstm.Read(VbufferDeLectura, 0, VbufferDeLectura.Length)

            'Se evalúa que llega en la cabecera del mensaje.
            Me.Inter.interpretar(Encoding.ASCII.GetString(VbufferDeLectura))

        End While
    End Sub

这里是类解释器,它将尝试理解字符串。

Public Class Interprete

Property Codigo As String
Property Cotizacion As Integer
Property Cabecera As String

Private VAcciones As DataTable
Public Property Acciones() As DataTable
    Get
        Return VAcciones
    End Get
    Set(ByVal value As DataTable)
        VAcciones = value
    End Set
End Property

Public Sub interpretar(Data As String)
    Me.Cabecera = Nothing
    Me.Codigo = Nothing
    Dim buff() As String = Split(Data, "!!")
    Dim arr() As String = Split(buff(0), "||")

    Me.Cabecera = arr(0)
    Me.Codigo = arr(1)
    Select Case Cabecera
        Case "COT"
            Cotizaciones(arr)
    End Select
End Sub
Public Sub Cotizaciones(M As String())
    Dim DR As DataRow = Acciones.NewRow
    Dim buffer(3) As Object
    buffer(0) = ""
    buffer(1) = 0
    buffer(2) = M(1)
    buffer(3) = M(2)
    DR.ItemArray = buffer
    Acciones.Rows.Add(DR)
End Sub
End Class

如何安全地使用 datagridview? 各位能推荐一下vb的书吗?

【问题讨论】:

  • 正如 LarsTech 回答的那样,在向DataTable 添加新行时,您需要BeginInvokeInvoke。在非UI线程上新建DataRows会间接导致DataGridView发生变化,会导致意想不到的结果。所以你不能使用共享的DataTable

标签: vb.net multithreading winforms datagridview thread-safety


【解决方案1】:

您应该在 GUI 线程上调用该数据表操作:

Me.BeginInvoke(New Action(Sub()
                            Dim DR As DataRow = Acciones.NewRow
                            Dim buffer(3) As Object
                            buffer(0) = ""
                            buffer(1) = 0
                            buffer(2) = M(1)
                            buffer(3) = M(2)
                            DR.ItemArray = buffer
                            Acciones.Rows.Add(DR)
                          End Sub))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 2014-01-06
    • 2022-11-21
    相关资源
    最近更新 更多