none
How can thread safe for DataGridView RRS feed

Answers

  • Windows controls can't have certain features modified outside the thread they were created in. Usually, this is the main GUI thread. In order to perform thread-safe operations on Windows Form Controls in another thread, there are 3 main ways to do so in a thread-safe fashion. The way I chose to do it is via the Invoke() method of the control. You can check to see if the control needs to be invoke by checking to see if Control.InvokeRequired is true.

    DataGridView controls are nasty in particular like a few other controls since their items can't derived from the Windows.Forms.Control class. This means they can't be invoked. For example, a DataGridView row which you want to change a GUI component of (i.e. the state of a check box). In this case, you have to invoke the DataGridView first then perform the changes on the row. This technique is also usefull for ToolstripMenuItems, TreeView nodes etc. Of course, if the DataGridViewRow isn't currently in a DataGridView (i.e. it's parent/table is NULL), you can operate on it instantly.

    Steps you need to take to invoke controls:

    -Have a delegate method prototype
    -Check if the control needs to be invoked
    -Create a function which its parameters and return type match the delegate
    -Call the Control.Invoke method and parse the delegate along with an object array of parameters which match the delegate parameter types

    For some good examples on how to do this and when to do it, check out Control.Invoke on MSDN.
    • Marked as answer by Zhi-Xin Ye Thursday, July 16, 2009 2:15 PM
    Sunday, July 12, 2009 10:19 AM

All replies

  • You need to take a look at the stack code.  See that block of text under the buttons?  That's where the important stuff is.  Post that here.
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    Friday, July 10, 2009 2:53 PM
    Moderator
  • You cannot access a DGV in a thread.  You cannot update data that's bound to a DGV in a thread.  Trying to do so anyway causes random failure.  Remove Control.CheckForIllegalCrossThreadCalls property assignments from your code.  Use Control.Invoke() to marshal a call to the UI thread.

    Hans Passant.
    Friday, July 10, 2009 3:01 PM
    Moderator
  • Windows controls can't have certain features modified outside the thread they were created in. Usually, this is the main GUI thread. In order to perform thread-safe operations on Windows Form Controls in another thread, there are 3 main ways to do so in a thread-safe fashion. The way I chose to do it is via the Invoke() method of the control. You can check to see if the control needs to be invoke by checking to see if Control.InvokeRequired is true.

    DataGridView controls are nasty in particular like a few other controls since their items can't derived from the Windows.Forms.Control class. This means they can't be invoked. For example, a DataGridView row which you want to change a GUI component of (i.e. the state of a check box). In this case, you have to invoke the DataGridView first then perform the changes on the row. This technique is also usefull for ToolstripMenuItems, TreeView nodes etc. Of course, if the DataGridViewRow isn't currently in a DataGridView (i.e. it's parent/table is NULL), you can operate on it instantly.

    Steps you need to take to invoke controls:

    -Have a delegate method prototype
    -Check if the control needs to be invoked
    -Create a function which its parameters and return type match the delegate
    -Call the Control.Invoke method and parse the delegate along with an object array of parameters which match the delegate parameter types

    For some good examples on how to do this and when to do it, check out Control.Invoke on MSDN.
    • Marked as answer by Zhi-Xin Ye Thursday, July 16, 2009 2:15 PM
    Sunday, July 12, 2009 10:19 AM