异步委托数据绑定!
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;
}