错误:在一个线程上创建的控件不能成为在另一个线程控件的父级(处理方法)

using System;
using System.Data;
using System.Threading;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace MultiThreadOperateControls
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  
  private delegate void BindDataGridDelegate();  // 创建委托和委托对象
  private BindDataGridDelegate bindDataGridDelegate;

  Thread bindGridThread;

  private System.Windows.Forms.Button btnErrorHandle;
  private System.Windows.Forms.Button btnSuccessHandle;
  private System.Windows.Forms.DataGrid dataGrid1;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   bindDataGridDelegate = new BindDataGridDelegate(BindDataGrid); // 实例化委托对象并指定调用的方法

   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.btnErrorHandle = new System.Windows.Forms.Button();
   this.btnSuccessHandle = new System.Windows.Forms.Button();
   this.dataGrid1 = new System.Windows.Forms.DataGrid();
   ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
   this.SuspendLayout();
   //
   // btnErrorHandle
   //
   this.btnErrorHandle.Location = new System.Drawing.Point(40, 24);
   this.btnErrorHandle.Name = "btnErrorHandle";
   this.btnErrorHandle.Size = new System.Drawing.Size(112, 40);
   this.btnErrorHandle.TabIndex = 0;
   this.btnErrorHandle.Text = "ErrorHandle";
   this.btnErrorHandle.Click += new System.EventHandler(this.btnErrorHandle_Click);
   //
   // btnSuccessHandle
   //
   this.btnSuccessHandle.Location = new System.Drawing.Point(232, 24);
   this.btnSuccessHandle.Name = "btnSuccessHandle";
   this.btnSuccessHandle.Size = new System.Drawing.Size(112, 40);
   this.btnSuccessHandle.TabIndex = 1;
   this.btnSuccessHandle.Text = "SuccessHandle";
   this.btnSuccessHandle.Click += new System.EventHandler(this.btnSuccessHandle_Click);
   //
   // dataGrid1
   //
   this.dataGrid1.DataMember = "";
   this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
   this.dataGrid1.Location = new System.Drawing.Point(32, 88);
   this.dataGrid1.Name = "dataGrid1";
   this.dataGrid1.Size = new System.Drawing.Size(336, 176);
   this.dataGrid1.TabIndex = 2;
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(408, 301);
   this.Controls.Add(this.dataGrid1);
   this.Controls.Add(this.btnSuccessHandle);
   this.Controls.Add(this.btnErrorHandle);
   this.Name = "Form1";
   this.Text = "多线程窗体控件处理事例";
   this.Load += new System.EventHandler(this.Form1_Load);
   ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   BindDataGrid();
  }

  private void btnErrorHandle_Click(object sender, System.EventArgs e)
  {
   StopBindThread();
   bindGridThread = new Thread(new ThreadStart(BindDataGrid)); // 直接调用非此线程创建的控件的操作 抛出异常
   bindGridThread.Start();  
  }
  private void btnSuccessHandle_Click(object sender, System.EventArgs e)
  {   
   StopBindThread();
   bindGridThread = new Thread(new ThreadStart(InvokeBindDataGrid)); // 通过委托调用,合法
   bindGridThread.Start();    
  }

  private void BindDataGrid()
  {
   int dataGridItemLenght = 5;
   DataTable dt = new DataTable("DataGridSource");
   dt.Columns.Add("RandomValue");
   for(int i=0;i<dataGridItemLenght;i++)
   {
    Random r = new Random();
    DataRow dr = dt.NewRow();
    dr[0] = r.Next(1,500);
    dt.Rows.Add(dr);
   }

   try
   {
    dataGrid1.DataSource = dt;
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
  }
  
  private void InvokeBindDataGrid()  // 调用非此线程创建的控件的操作必须用 Invoke 或 BeginInvoke .否则将抛出异常
  {  
   dataGrid1.Invoke(bindDataGridDelegate,null);   
  }
  private void StopBindThread()
  {
   if((bindGridThread != null)&&(bindGridThread.IsAlive))
   {
    bindGridThread.Abort();
    bindGridThread.Join();
   }
   bindGridThread = null;
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值