zoukankan      html  css  js  c++  java
  • 简单的异步调用

    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;
            }
        }
    }
  • 相关阅读:
    IOS 自动布局-UIStackPanel和UIGridPanel(三)
    IOS 自动布局-UIStackPanel和UIGridPanel(二)
    IOS 自动布局-UIStackPanel和UIGridPanel(一)
    我的新博客
    Topcoder SRM655 DIV1 500 Nine
    BestCoder Round #38 1002 Greatest Greatest Common Divisor 筛法
    BestCoder Round #38 1001 Four Inages Strategy 暴力
    Google Code Jam 2015 Round 1A Haircut 二分
    Google Code Jam 2015 Round 1A Mushroom Monster 水
    Topcoder SRM656 DIV1 250 RandomPancakeStack 概率DP
  • 原文地址:https://www.cnblogs.com/perfect/p/1564291.html
Copyright © 2011-2022 走看看