i got a background worker that has the following code in the do work.... one of the things i want to do is to add rows if theres none available ...but i got an error saying i need to do a safe call thread...i already read about it but im stuck i put control.invoke, but that gives me an error too saying Error 1 Reference to a non-shared member requires an object reference.


so i need help what can i do to add rows safely

Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
    
                        If DataGridView2.Rows.Item(num).Cells.Item(0).Value = "" Then
                            Control.Invoke(DataGridView2.Rows.Add(1))

Use SyncLock...End SyncLock to prevent other threads to update rows at the same time

Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
    
SyncLock DataGridView2.Rows
  If DataGridView2.Rows.Item(num).Cells.Item(0).Value = "" Then DataGridView2.Rows.Add(1)
End SyncLock

Read more about the SyncLock Statement in the MSDN.

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.