Threaded application, DataTables and DataGrids

G

Guest

Hi,

I'm novice with C# and have a little problem in my current project.
I'm updating some DataTable from different Thread. There's no problem to update the DB, but adding new records freeze the application because I've a datagrid showing the updates in realtime...
Principe :
myDataGrid.DataSource = new DataView(myDataTable);

After a lot of tests, it appears to be the fact of new rows created in another thread.

What can I do to go through this?
 
W

William Ryan eMVP

I'm going to say this with a slight caution... with threading there can be
multiple issues, but I think what you'll want to do is sync (C#) or SyncLock
the datatable as you are doing your additions. The grid may be another
issue.

Remember that shared/static methods of the datagrid are thread safe, but
instance methods aren't. You're using instance methods here. Similiarly,
DataTables are safe for multithreaded reads but not writes. Adding a row is
a write so you need to synchronize it.

HTH,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
sgellenoncourt said:
Hi,

I'm novice with C# and have a little problem in my current project.
I'm updating some DataTable from different Thread. There's no problem to
update the DB, but adding new records freeze the application because I've a
datagrid showing the updates in realtime...
 
M

Miha Markic [MVP C#]

Hi,

In addition to Bill.
Don't ever never touch UI controls from within another thread.
You should really read excellent article from Chris Sells that explains the
proper way:
Safe, Simple Multithreading in Windows Forms, Part 3
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms01232003.asp

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

sgellenoncourt said:
Hi,

I'm novice with C# and have a little problem in my current project.
I'm updating some DataTable from different Thread. There's no problem to
update the DB, but adding new records freeze the application because I've a
datagrid showing the updates in realtime...
 
W

William Ryan eMVP

Miha - you are the coolest person in the world. I was looking for this for
the last day and couldn't find it.

--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
Miha Markic said:
Hi,

In addition to Bill.
Don't ever never touch UI controls from within another thread.
You should really read excellent article from Chris Sells that explains the
proper way:
Safe, Simple Multithreading in Windows Forms, Part 3
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms01232003.asp

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Hi,

I'm novice with C# and have a little problem in my current project.
I'm updating some DataTable from different Thread. There's no problem to
update the DB, but adding new records freeze the application because I've a
datagrid showing the updates in realtime...
Principe :
myDataGrid.DataSource = new DataView(myDataTable);

After a lot of tests, it appears to be the fact of new rows created in another thread.

What can I do to go through this?
 

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