SilverBackApe
SilverBackApe

Reputation: 247

C# Adding to BindingList causes Operation is not valid due to the current state of the object

I'm working on a program that is adding to a couple of BindingLists in 2 separate threads. The action of adding to these BindingLists adds rows to 2 separate DatagridViews. As I want both DataGridViews to update concurrently, both threads are started in the same button click method. If I start only one of the threads, a single DataGridView updates just fine with no errors. However, if I start both threads in the method, I get the following error:

A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Additional information: Operation is not valid due to the current state of the object.

This however does not happen in every run. If I run the code 10 times this error might happen 4 or 5 out of the 10 times while the other 5 runs run perfectly fine.

This is a fairly big program so I will post the snippets that are directly involved. An extra eye would greatly be appreciated in case you can see something I am not seeing.

Method which creates the BindingLists

private void GridForm_Load(object sender, EventArgs e)
{

            spreadRows = new List<SpreadData>();
            positionRows = new List<PositionData>();
            spreadPositionRows = new List<PositionData>();   

            spreadBList = new BindingList<SpreadData>(spreadRows);
            positionBlist = new BindingList<PositionData>(positionRows);
            spreadPositionBlist = new BindingList<PositionData>(spreadPositionRows);


            this.spreadDataBindingSource.DataSource = spreadBList;
            this.dataGridView5.DataSource = this.spreadDataBindingSource;

            this.positionDataBindingSource.DataSource = positionBlist;
            this.dataGridView3.DataSource = this.positionDataBindingSource;

            this.positionDataBindingSource1.DataSource = spreadPositionBlist;
            this.dataGridView2.DataSource = this.positionDataBindingSource1;


            this.positionDataBindingSource1.ListChanged += new ListChangedEventHandler(positionDataBindingSource1_ListChanged);
            this.positionDataBindingSource.ListChanged += new ListChangedEventHandler(positionDataBindingSource_ListChanged);

            .......
        }

Method that creates the threads:

private void button3_Click(object sender, EventArgs e)
        {
             ......

            Thread t = new Thread(retrievePositionData);
            t.Start();

            Thread f = new Thread(retrieveSpreadPositionData);
            f.Start();

        }

Method that adds to 1st BindingList:

private void retrievePositionData()
{
    PositionData theData2;

    while (true)
    {
        .........

        char[] delimeter2 = { '+' };

        string[] text2 = reply2.Split(delimeter2);                   

        theData2 = new PositionData();

        theData2.uniqueId = text2[1];
        theData2.Security = text2[2];

        this.BeginInvoke((MethodInvoker)delegate() { positionBlist.Add(theData2); });

        ......

     }

}

Method that adds to the second BindingList:

 private void retrieveSpreadPositionData()
 {
     PositionData theData2;

     while (true)
     {
        ......

        char[] delimeter2 = { '+' };

        string[] text2 = reply2.Split(delimeter2);              

        theData2 = new PositionData();

        theData2.uniqueId = text2[1];
        theData2.Spread_Price = text2[3];

        this.BeginInvoke((MethodInvoker)delegate() { spreadPositionBlist.Add(theData2); });

        .......

      }
}

The program stops at the this.BeginInvoke((MethodInvoker)delegate() { positionBlist.Add(theData2); }); line of the retrievePositionData() method. And this only happens when both threads are started. If only one thread is started in the button click method, there are no problems.

Thanks in advance for your help.

Upvotes: 4

Views: 1176

Answers (0)

Related Questions