// private void CalcCallback(IAsyncResult ar) //{ // // Following line is used when this callback method can not acquire a handle of the delegate, mDeleg, for // // example, when it is in another class. The AsyncDelegate property was generated by the compiler together // // with the BeginInvoke and EndInvoke methods. // // CalcDelegate mDeleg = (CalcDelegate)(ar.AsyncDelegate); // string strResult = null; // int iResult = mDeleg.EndInvoke(ref strResult, ar); // tbResult.Text = Convert.ToString(iResult); // the third textbox // tbMsg.Text = strResult; // the message textbox // int i = (int)ar.AsyncState; // MessageBox.Show("The ar.AsyncState is " + i); // will show 123456789 // } private void Button_Click_1(object sender, RoutedEventArgs e) { //mDeleg = new CalcDelegate(Calc); //AsyncCallback callback = new AsyncCallback(CalcCallback); //int input1 = Convert.ToInt16(tbInput1.Text); //int input2 = Convert.ToInt16(tbInput2.Text); //string strResult = null; // dummy parameter //mDeleg.BeginInvoke(input1, input2, ref strResult, callback, 123456789); CalcDelegate deleg = new CalcDelegate(Calc); //AsyncCallback callback = new AsyncCallback(CalcCallback); int input1 = Convert.ToInt16(tbInput1.Text); int input2 = Convert.ToInt16(tbInput2.Text); string strResult = null; // dummy parameter IAsyncResult ar = deleg.BeginInvoke(input1, input2, ref strResult, null, new object()); //deleg.BeginInvoke(input1, input2, ref strResult, callback, new object()); ar.AsyncWaitHandle.WaitOne(); strResult = null; int iResult = deleg.EndInvoke(ref strResult, ar); tbResult.Text = Convert.ToString(iResult); tbMsg.Text = strResult; }
void CalcCallback(IAsyncResult asyncRes) { endtime = DateTime.Now; AsyncResult ares = (AsyncResult)asyncRes; CalcDelegate delg = (CalcDelegate)ares.AsyncDelegate; string answer = delg.EndInvoke(asyncRes); gAnswer = answer; }