Q:

Cross threading issue updating data source

Goal:
Once i click on the start button on my user interface, i currently fire two background workers.
BGWorker 1 is responsible for invoking the .dll method to crawl a website
BGWorker 2 is responsible for updating the DataGridView using the Datasource = source setter
Issues:
My dll receives as argument, the reference to the Data Source that will be populated while the .dll is running. So Basically, the .dll is going to add a object in the data source once in a while, until it finishes running.
The BGWorker 2 does this :
while (1 == 1) // Runs until is it manually disposed
{
    CoursesGrid.BeginInvoke // inline method 
    (
        (Action) delegate 
        {
            if (_coursesSource.Count > 0) // if source is not empty,Binds.
            {
                try
                { 
                    CoursesGrid.DataSource = _coursesSource;
                    CoursesGrid.EndEdit();
                }
                catch (Exception ex)
                {
                    Logs.LogWriter.LogError(ex);
                }                   
            }
            else
            {
                // Signals User To keep Waiting
            }
        }
     );
Thread.Sleep(4000);

Behavior flow:

Dll is Invoked
Method Parses a element and adds to the source
Eventually, BGWorker 2 Wakes up and Binds the DataGridView source to the modified Source
Dll threads keep parsing but fails once it tries to add a new element to the source

TL:DR:
Thread 1 Writes on the Source
Thread 2 Reads this modified source, and binds it to the DataGridView to it will refresh
Thread 1 Fails to write again on the source : 
"Cross-thread operation not valid: Control 'CoursesGrid' accessed from a thread other than the thread it was created on."}

Both threads are handling the same source, but the BGWorker 2, only binds it to the interface, this should be working. Any idea of what might be happening here?

A:

I think what is happening is that you are binding the data grid to your _coursesSource which causes the data grid to show the first row.  Then, later, you are modifying the same _coursesSource instance which likely fires a property change or collection change notification which causes the data grid to attempt to update itself (ie. show the newly added row).
But since the modification is happening on another thread the event which the data grid "hears" and responds to also happens on that background thread which causes the cross thread violation.
What you need to do, is either a) marshal the add to _coursesSource onto the UI thread (which it doesn't sound like you  can easily do), or b) bind to a copy of _coursesSource and then in your BGWorker 2, each time you update _coursesSource re-bind the grid to a new copy, that way the grid is never "hearing" change notifications, it's always just binding to a fresh copy of the collection.  It's not the most efficient way to do things, but it should get the job done for you.

