Hello all,
Pardon the topic name, but this error has been giving us a lot of trouble and after weeks of investigating we still have no idea how, when, or why it happens.
We use background workers but the grid that's getting this error is independent of the BG.
If someone can please tell us when the error happens so we can trap it, that would be great.
Attached is the error message we're getting when the "Red X" happens.
Thank you very much.
I had a similar problem (different exception, but same red X) in my application. We had multiple threads updating the datasource on a different thread than the main UI thread that the grid was on. We fixed the problem by moving all datasource updates to the same thread as the grid (as Mike S. suggested) using Invoke().
I solved all these problems in one place. I made a custom control that inherits BindingSource and is thread-safe. Code is below:
public partial class MyBindingSource : BindingSource
{
InitializeComponent();
}
private static extern IntPtr GetForegroundWindow();
var control = Form.FromHandle(handle);
control.Invoke(new Action<ListChangedEventArgs>(OnListChanged), e);
else
I think this is rare. The most common problem is when you set the data not in the main thread and this Invokes the ListChanged event. If the grid access the data, then everything is on the main thread so there won't be any problem.
Anyhow, my code solves most of the problems.
What you are doing here will certainly handle the ListChanged notifications. But it's not a cure-all for threading issues. It does not, for example, affect what happens if the grid tries to access the data at any time outside of a ListChanged event. The grid may attempt to access the data source at any time. It might try to get the count of the rows or the data structure or access a ListObject for a row to update the data and there's no garuantee that the data source will be in the proper state at that point.