using System;
using System.Data;
using System.Threading;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace MultiThreadOperateControls
{
///
/// Form1 的摘要说明。
///
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;
///
/// 必需的设计器变量。
///
private System.ComponentModel.Container components = null;
public Form1()
{
bindDataGridDelegate = new BindDataGridDelegate(BindDataGrid); // 实例化委托对象并指定调用的方法
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
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
///
/// 应用程序的主入口点。
///
[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
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;
}
}
}