DataGrid crashes when data comes from sub threads
-
hi, i have a datagrid bound on a dataset which i fill whith a method running in a threadpool. showed simple:
private void begin(object sender, System.EventArgs e) { ThreadPool.QueueUserWorkItem( new WaitCallback( work ) ); } public void work( object n ) { for ( int i = 0; i < 20; i++) { lock(this) { DataLayer.add( i ); } } }
after 3 items or when the thread ends the programm hangs or im getting a exeption on Appliaction.Run(). how can i do that without a programm crash? i have read on MSDN DataSet doc "You must synchronize any write operations" but it seems the to be the datagrid? i also have tryed a workaround whit delegates, events and a static adding but same problems here. i can't find the problem, i think i know not enough about this threadsave stuff. any idea how to do that, or is it simply not possible to fill a datagrid from a thread? -
hi, i have a datagrid bound on a dataset which i fill whith a method running in a threadpool. showed simple:
private void begin(object sender, System.EventArgs e) { ThreadPool.QueueUserWorkItem( new WaitCallback( work ) ); } public void work( object n ) { for ( int i = 0; i < 20; i++) { lock(this) { DataLayer.add( i ); } } }
after 3 items or when the thread ends the programm hangs or im getting a exeption on Appliaction.Run(). how can i do that without a programm crash? i have read on MSDN DataSet doc "You must synchronize any write operations" but it seems the to be the datagrid? i also have tryed a workaround whit delegates, events and a static adding but same problems here. i can't find the problem, i think i know not enough about this threadsave stuff. any idea how to do that, or is it simply not possible to fill a datagrid from a thread?Two things, you should synchronize any writes by either using the
lock
keyword in C# (type it in your help index to learn more about it) or some other class inSystem.Thread
(lock
aliases theMonitor
class). Second, when you modify the UI in most cases, you should invoke the method on the control usingInvoke
, which is inheritted by all controls from theControl
class. There's a niftInvokeRequired
that you can use to determine if you can call the method directly or if you need to have the control invoke it from its creation thread. Every control is created on a thread and all changes to that control (at least, that which would be reflected in the underlying native Windows control) need to be invoked on that thread. Read the docs for theControl.Invoke
andControl.InvokeRequired
in the .NET Framework SDK for more information and examples of how to use them.Microsoft MVP, Visual C# My Articles
-
Two things, you should synchronize any writes by either using the
lock
keyword in C# (type it in your help index to learn more about it) or some other class inSystem.Thread
(lock
aliases theMonitor
class). Second, when you modify the UI in most cases, you should invoke the method on the control usingInvoke
, which is inheritted by all controls from theControl
class. There's a niftInvokeRequired
that you can use to determine if you can call the method directly or if you need to have the control invoke it from its creation thread. Every control is created on a thread and all changes to that control (at least, that which would be reflected in the underlying native Windows control) need to be invoked on that thread. Read the docs for theControl.Invoke
andControl.InvokeRequired
in the .NET Framework SDK for more information and examples of how to use them.Microsoft MVP, Visual C# My Articles
lock + begininvoke works great now, thanks a lot! unbelivable that the solution is so easy... :-D