DataTable/DataGrid object and multiple threads

D

Dr. Len

Hi all!
In my app I implemented a separate worker thread that handles network
communication using Net.Sockets. It needs to make updates to a
DataTable object, which is displayed to the user by a DataGrid
control. The DataTable object is a 'public static' field instantiated
within context of the main thread.

The problem is that if I add a column to the DataTable from within the
worker thread (eg. different thread than where the DataTable and
DataGrid reside in), all columns suddenly disappear from the DataGrid
view for some reason;

Controller.dataTable.Columns.Add(new DataColumn("TestColumn"));

If I move this code to the main thread, it works fine. After adding
new column, I looked at dataTable with debugger and it seemed fine. So
the culprit must be the DataGrid control. Do I need some kind of
thread synchronization?
 
G

Guest

Instead of trying to update the DataTable directly, raise an event from the
worker thread, passing the data in the event routine and let the main UI
thread handle updating the control.

Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top