private void button1_Click(object sender, EventArgs e) { //调用委托实例的BeginInvoke方法来异步执行委托实例上绑定的方法 asyncChangeText changeText = new asyncChangeText(ChangeTextNoArgs); //changeText.BeginInvoke(null, null); //异步委托的回调 MyEventArgs ret = new MyEventArgs(); Console.WriteLine("1 " + getMemory(ret)); IAsyncResult rst = changeText.BeginInvoke(AssistClass.count.ToString(), ref ret, new AsyncCallback(CallbackMethod), "This is msgBox callBack!"); }
// The callback method must have the same signature as the // AsyncCallback delegate. static void CallbackMethod(IAsyncResult ar) { // Retrieve the delegate. AsyncResult result = (AsyncResult)ar; asyncChangeText caller = (asyncChangeText)result.AsyncDelegate; // Retrieve the format string that was passed as state // information. string rst = (string)ar.AsyncState; // Define a variable to receive the value of the out parameter. // If the parameter were ref rather than out then it would have to // be a class-level field so it could also be passed to BeginInvoke. MyEventArgs outStr = new MyEventArgs(); Console.WriteLine("2 " + getMemory(outStr)); // Call EndInvoke to retrieve the results. bool returnValue = caller.EndInvoke(ref outStr, result); MessageBox.Show(rst + "\r\n" + outStr + "\r\n" + returnValue.ToString()); Console.WriteLine("3 " + getMemory(outStr)); }