• 简单的异步调用


    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;
            }
        }
    }
  • 相关阅读:
    动词的形态及变化(转)
    数论基础
    P1505 [国家集训队]旅游
    贪心常见题
    主席树
    卡常火车头
    AC自动机
    左偏树
    位运算
    Linux下Vim常用操作
  • 原文地址:https://www.cnblogs.com/perfect/p/1564291.html
Copyright © 2020-2023  润新知