CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2013
    Posts
    4

    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.

  2. #2
    Join Date
    May 2013
    Posts
    4

    Re: DataGridView and Threading - Desperate Help Needed

    So I figured out part of the problem. The following line of code in the SetDataTable function is not working. It gets executed in the thread but results in an untouched table. I thought this might be a synchronization problem with the table so I locked it for each thread, but no go. Still doesn't work.

    dt.Rows.Add(dr)

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: DataGridView and Threading - Desperate Help Needed

    If you have a key, you'll NEED the key#
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    May 2013
    Posts
    4

    Re: DataGridView and Threading - Desperate Help Needed

    Quote Originally Posted by dglienna View Post
    If you have a key, you'll NEED the key#
    Thanks, but I'm not using keys...should I be? I don't have any use for them unless they are necessary.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: DataGridView and Threading - Desperate Help Needed

    Well, if you'll ever have more than 100 or so records in the table, you don't need one. Anything else will be sped up IMMENSELY. Plus, you can have multiple keys in the same record. What happens when the table is locked in one thread, and the other has to wait? I guess you figured that out.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    May 2013
    Posts
    4

    Re: DataGridView and Threading - Desperate Help Needed

    Quote Originally Posted by dglienna View Post
    Well, if you'll ever have more than 100 or so records in the table, you don't need one. Anything else will be sped up IMMENSELY. Plus, you can have multiple keys in the same record. What happens when the table is locked in one thread, and the other has to wait? I guess you figured that out.
    Per thread, there is only one row in the table and only 4 items per row. That's just the problem, no matter how I change the code, in the debugger everything looks good but the table row never gets transferred when using multithreading. If I do it without multithreading it works fine, but of course it locks up the gui as everything is taking place on the main thread. I am not a seasoned .net programmer for sure so I'm probably just missing something simple.

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
  •  





Click Here to Expand Forum to Full Width

Featured