The Perfect Day

分享技术,编写未来

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinAppAsync
{
    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
        }

        
private void btnLoad_Click(object sender, EventArgs e)
        {
            
this.Text = "正在加载";
            
//异步执行
            LoadDataHandlerInstance = new LoadDataHandler(CreateData);
            AsyncCallback callBackMethod 
= new AsyncCallback(CallBackLoad);
            LoadDataHandlerInstance.BeginInvoke(callBackMethod, LoadDataHandlerInstance);
        }

        
public delegate DataTable LoadDataHandler();
        
public LoadDataHandler LoadDataHandlerInstance = null;
        
private DataTable CreateData()
        {
            DataTable dt 
= new DataTable();
            dt.Columns.Add(
"Id"typeof(string));
            dt.Columns.Add(
"Name"typeof(string));
            dt.Columns.Add(
"Address"typeof(string));

            
for (int i = 0; i < 800000; i++)
            {
                DataRow row 
= dt.NewRow();
                row[
"Id"= i.ToString();
                row[
"Name"= "Name_" + i.ToString();
                row[
"Address"= "Address_" + i.ToString();
                dt.Rows.Add(row);
            }
            
return dt;
        }
        
public void CallBackLoad(IAsyncResult result)
        {
            LoadDataHandler loadInstance 
= (LoadDataHandler)result.AsyncState;
            DataTable dt 
= loadInstance.EndInvoke(result);

            bindGridHandlerInstance 
= new BindGridHandler(BindGrid);
            
this.dgv.BeginInvoke(bindGridHandlerInstance, new object[] { dt });//执行控件的Invoke或BeginInvoke以修改主线程上的属性
        }

        
public delegate void BindGridHandler(DataTable dt);
        
public BindGridHandler bindGridHandlerInstance = null;
        
private void BindGrid(DataTable dt)
        {
            
this.dgv.DataSource = dt;
        }
    }
}
posted on 2009-09-10 19:46  StephenJu  阅读(261)  评论(0编辑  收藏  举报