zoukankan      html  css  js  c++  java
  • DataGridView 在多线程中使用可能出现大红叉

    老外的解释:
    The DataGridView is a common .Net control used to display and permit editing of tabular data. It can be filled in via code or by attaching a data source to it.
    DataGridViews, like most controls, are not thread-safe. That is, you need to perform operations on them using the same thread as they were created on. However, sometimes OpenSpan developers will accidently modify the DataGridView from another thread because the operation is coming from an event in another application. 
    A safe way to correct this problem and to ensure it doesn't accidently happen is to postpone updating the control during a OnPaint() event if it happens from another thread. This is done by handling an exception and then flagging the control to be redrawn when the MessagePump is run. You can do this by creating a .Net class that inherits from DataGridView and overrides the OnPaint() event. You will need access to a C# compiler to build this. Once built, you add it to the OpenSpan Studio Toolbox and replace your DataGridView controls with it.
    (引用地址:http://wenku.baidu.com/link?url=uLLKAHMKVzub20zu--98KrBOsUnMB5qIa10CXaBjhq-AKHDLifA3V8j5gpM996vglpIq1-4aq26X7-rNJ-t_EbIEqtNtet0TT6rnVRYJh7G

    处理的方法,重新封装DataGridView 控件,重新OnPaint 方法,代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Windows.Forms;
    
    
    
    namespace test
    {
        class DataGridViewNew : DataGridView
        {
            protected override void OnPaint(PaintEventArgs e)
            {
                try
                {
                    base.OnPaint(e);
                }
                catch
                {
    
                    Invalidate();
                }
            }
    
        }
    }

    使用封装的控件以后,经过多次测试,没有再出现大红叉!

    为API生,为框架死,为debug奋斗一辈子;吃符号亏,上大小写的当,最后死在需求上。
  • 相关阅读:
    代码解析&Filter用户授权例子
    session
    软件工程结对作业2
    软件工程结对作业1
    软件工程第三次作业
    软件工程第二次作业
    软件工程第一次作业
    KMP算法
    哈希表
    Mysql事物隔离级别
  • 原文地址:https://www.cnblogs.com/ChaunceyWan/p/13533914.html
Copyright © 2011-2022 走看看