-
May 30th, 2013, 06:22 AM
#1
DataGridView and Threading - Desperate Help Needed
Hi all,
This is my first post so please be gentle, lol. So I've spent almost a week rewriting this code umpteen different ways and still cannot get it to work with threading. I can get it to work without multithreading, no problem. As soon as I thread it, even with a delegate, it breaks. Please, if you can shed some light on the problem I would be most grateful.
I've purposely left out the delegate here hoping someone might see what's what. Basically I am trying to update a DGV with a row created in a thread. Class2 holds the function, setData, being called on the thread and Form1 holds the DGV. SetDataTable is the actual function in Form1 that sets the row's data in the DGV and gets called in Class2(I can do this right?). I've abridged the code for simplicity's sake. Please help.
Code:
Public Class Class2
Public Sub setData()
Dim dt1 As New DataTable()
dt1.Columns.Add(New DataColumn("", GetType(String)))
dt1.Columns.Add(New DataColumn("", GetType(String)))
dt1.Columns.Add(New DataColumn("", GetType(String)))
dt1.Columns.Add(New DataColumn("", GetType(String)))
dt1.Rows.Add()
Dim DataR As DataRow
Dim rowArray(4) As Object
DataR = dt1.Rows(0)
DataR.Item(0) = ipAddr
DataR.Item(1) = iptohost(ipAddr)
DataR.Item(2) = getMACAddress(ipAddr)
DataR.Item(3) = "True"
dt1.ImportRow(DataR)
Form1.SetDataTable(dt1)
End Sub
End class
'--------------------------------------------------------------
Public Class Form1
Dim dt As New DataTable()
Dim dr As DataRow
Private Sub Initialize()
dt.Columns.Add(New DataColumn("ComputerIP", GetType(String))) '0
dt.Columns.Add(New DataColumn("ComputerName", GetType(String))) '1
dt.Columns.Add(New DataColumn("ComputerMAC", GetType(String))) '2
dt.Columns.Add(New DataColumn("Bandwidth", GetType(String)))
dg1.DataSource = dt
dr = dt.NewRow()
End Sub
'----------------------------------------------------------------
Public Sub SetDataTable(ByVal table As DataTable)
Try
dr.ItemArray = table.Rows(0).ItemArray
dt.Rows.Add(dr)
dg1.Refresh()
Catch e As Exception
Debug.Write(e)
End Try
End If
End Sub
'----------------------------------------------------------------
Public Sub dosomething()
GetComp as Class2
GetComp = New Class2(pertinentinfo)
Dim doThread As New Thread(AddressOf GetComp.setData)
doThread.Start()
End Sub
'-----------------------------------------------------------------
Public Sub New()
' This call is required by the designer.
InitializeComponent()
Initialize()
' Add any initialization after the InitializeComponent() call.
End Sub
End Class
Last edited by GremlinSA; May 30th, 2013 at 12:08 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|