• 异步委托数据绑定!


    private void button1_Click(object sender, EventArgs e)
    {
        GetLogDelegate getLogDel = new GetLogDelegate(GetLogs);
       
        getLogDel.BeginInvoke(new AsyncCallback(LogTableCallBack),  null);
    }

    public delegate DataTable GetLogDelegate();

    /// <summary>
    /// 从数据库中获取操作日志,该操作耗费时间较长,
    /// 且返回数据量较大,日志记录可能超过万条。
    /// </summary>
    /// <returns></returns>
    private DataTable GetLogs()
    {
        string sql = "select * from ***";
        DataSet ds = new DataSet();

        using (OracleConnection cn = new OracleConnection(connectionString))
        {
            cn.Open();

            OracleCommand cmd = new OracleCommand(sql, cn);

            OracleDataAdapter adapter = new OracleDataAdapter(cmd);
            adapter.Fill(ds);
        }

        return ds.Tables[0];
    }

    /// <summary>
    /// 绑定日志到ListBox控件。
    /// </summary>
    /// <param name="tag"></param>
    private void LogTableCallBack(IAsyncResult tag)
    {
        AsyncResult result = (AsyncResult)tag;
        GetLogDelegate del = (GetLogDelegate)result.AsyncDelegate;

        DataTable logTable = del.EndInvoke(tag);

        if (this.listBox1.InvokeRequired)
        {
            this.listBox1.Invoke(new MethodInvoker(delegate()
            {
                BindLog(logTable);
            }));
        }
        else
        {
            BindLog(logTable);
        }
    }

    private void BindLog(DataTable logTable)
    {
        this.listBox1.DataSource = logTable;
    }

  • 相关阅读:
    POJ 3164 Command Network 最小树形图 朱刘算法
    区间dp专题
    HDU2896病毒入侵AC_自动机
    HDU2222Keywords Search AC_自动机
    Linux cat命令参数及使用方法详解
    MySQL分支Percona, cmake编译安装
    PHP网站简单架构 – 单独跑php-fpm
    Tengine – Nginx衍生版
    jemalloc优化MySQL、Nginx内存管理
    TCMalloc优化MySQL、Nginx、Redis内存管理
  • 原文地址:https://www.cnblogs.com/jordan2009/p/2995056.html
Copyright © 2020-2023  润新知