Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DataGrid crashes when data comes from sub threads

DataGrid crashes when data comes from sub threads

Scheduled Pinned Locked Moved C#
questionhelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Franz Pentenrieder
    wrote on last edited by
    #1

    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?

    H 1 Reply Last reply
    0
    • F Franz Pentenrieder

      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?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      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 in System.Thread (lock aliases the Monitor class). Second, when you modify the UI in most cases, you should invoke the method on the control using Invoke, which is inheritted by all controls from the Control class. There's a nift InvokeRequired 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 the Control.Invoke and Control.InvokeRequired in the .NET Framework SDK for more information and examples of how to use them.

      Microsoft MVP, Visual C# My Articles

      F 1 Reply Last reply
      0
      • H Heath Stewart

        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 in System.Thread (lock aliases the Monitor class). Second, when you modify the UI in most cases, you should invoke the method on the control using Invoke, which is inheritted by all controls from the Control class. There's a nift InvokeRequired 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 the Control.Invoke and Control.InvokeRequired in the .NET Framework SDK for more information and examples of how to use them.

        Microsoft MVP, Visual C# My Articles

        F Offline
        F Offline
        Franz Pentenrieder
        wrote on last edited by
        #3

        lock + begininvoke works great now, thanks a lot! unbelivable that the solution is so easy... :-D

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups