由于对象的当前状态,C#添加到BindingList导致操作无效
我正在开发一个程序,该程序在两个单独的线程中添加到两个由于对象的当前状态,C#添加到BindingList导致操作无效,c#,multithreading,datagridview,bindinglist,begininvoke,C#,Multithreading,Datagridview,Bindinglist,Begininvoke,我正在开发一个程序,该程序在两个单独的线程中添加到两个绑定列表中。添加到这些绑定列表中的操作将行添加到两个单独的DataGridView。由于我希望两个DataGridViews同时更新,因此两个线程都以相同的按钮单击方法启动。如果我只启动其中一个线程,那么一个DataGridView会正常更新,没有错误。但是,如果在方法中启动两个线程,则会出现以下错误: A first chance exception of type 'System.InvalidOperationException' oc
绑定列表中。添加到这些绑定列表中的操作将行添加到两个单独的DataGridView
。由于我希望两个DataGridViews
同时更新,因此两个线程都以相同的按钮单击方法启动。如果我只启动其中一个线程,那么一个DataGridView
会正常更新,没有错误。但是,如果在方法中启动两个线程,则会出现以下错误:
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.
然而,并非每次跑步都会发生这种情况。如果我将代码运行10次,那么在10次运行中,可能会出现4次或5次错误,而其他5次运行都很正常
这是一个相当大的程序,所以我将发布直接涉及的代码片段。如果你能看到我没有看到的东西,请多看一眼
创建绑定列表的方法
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);
.......
}
添加到第一个绑定列表的方法:
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); });
......
}
}
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); });
.......
}
}
方法添加到第二个绑定列表中:
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); });
......
}
}
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); });
.......
}
}
程序在this.BeginInvoke((MethodInvoker)delegate(){positionBlist.Add(theData2);})处停止代码>检索位置数据()方法的行。这只有在两个线程都启动时才会发生。如果按钮单击方法中只启动了一个线程,则不会出现问题
提前感谢你的帮助