BeginInvoke异步执行的问题
我现在做一个winform程序,有个要读数据的方法,因为时间可能较长,所以想用异步执行的方法去处理这个方法,代码如下:
this.BeginInvoke(new dlgGetData(getData), url, textBox1);
private void Form1_Load(object sender, EventArgs e)
{
Action<string> action = new Action<string>(DoWork);
AsyncCallback callback = new AsyncCallback(Callback);
action.BeginInvoke("你要传的参数", callback, action);
}
protected void Callback(IAsyncResult ar)
{
Action<string> dlgt = (Action<string>)ar.AsyncState;
dlgt.EndInvoke(ar);
}
protected void DoWork(string msg)
{
Thread.Sleep(1000);//耗时操作
Action<string> action=delegate{ label1.Text=msg;}; //界面更新
label1.Invoke(action,msg); //界面列新
}