I have been looking for BindingList Invoke from other thread few days ago but not able to get a proper solution in vb.net , most of them are in C# but i am finding difficult to understand that.
Hence i have created a small application which has 2 Forms (Form1 and Form2) and one Class, Form1 will be main UI thread and Form2 will be running on a different thread.
Form1 has a DataGrindView bound to shared BindingList(of T) and a Button, once the Button clicked Form2 will be loaded on a different thread.
Here is Form1 Codes :
Public Class Form1
Public Shared ListOfNames As BindingList(Of Names) = New BindingList(Of Names)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.DataSource = ListOfNames
DataGridView1.Columns("FullName").DataPropertyName = "FullName"
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim thread As Threading.Thread = New Threading.Thread(AddressOf loadQuoteForm)
Private Sub loadQuoteForm()
Dim SecondForm As Form2 = New Form2
Public Class Form2
Private Sub btnTestFromDiffTread_Click(sender As Object, e As EventArgs) Handles btnTestFromDiffTread.Click
Dim myName As Names = New Names
myName.FullName = "John Peter"
Catch ex As Exception
Public Class Names
Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private _fullName As String
Public Property FullName() As String
Set(ByVal value As String)
_fullName = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("FullName"))
It is updating of the DataGridView that is causing the issue.
From your comment I can see it is because the DataGridView is bound to DataSource of the DataGridView, so updating this List will cause a cross thread operation if you are doing this on a separate thread.
One solution is to not make
ListOfNames public and have a new method to allow adding to this list:
Private Shared ListOfNames As BindingList(Of Names) = New BindingList(Of Names) Public Sub AddNameToList(newNames As Names) DataGridView1.BeginInvoke(Sub() ListOfNames.Add(newNames)) End Sub