private void Callback(IAsyncResult asyncResult) { // Retrieve the proxy from state. EmployeesService proxy = (EmployeesService)asyncResult.AsyncState; // Complete the call. DataSet ds = proxy.EndGetEmployees(asyncResult); // Update the user interface on the right thread. this.Invoke(new UpdateGridDelegate(UpdateGrid), new object[] { ds }); }
private void cmdMultiple_Click(object sender, System.EventArgs e) { // Record the start time. DateTime startTime = DateTime.Now; EmployeesService proxy = new EmployeesService(); // Call three methods asynchronously. IAsyncResult async1 = proxy.BeginGetEmployees(null, null); IAsyncResult async2 = proxy.BeginGetEmployees(null, null); IAsyncResult async3 = proxy.BeginGetEmployees(null, null); // Create an array of WaitHandle objects. WaitHandle[] waitHandles = { async1.AsyncWaitHandle, async2.AsyncWaitHandle, async3.AsyncWaitHandle }; // Wait for all the calls to finish. WaitHandle.WaitAll(waitHandles); // You can now retrieve the results. DataSet ds1 = proxy.EndGetEmployees(async1); DataSet ds2 = proxy.EndGetEmployees(async2); DataSet ds3 = proxy.EndGetEmployees(async3); DataSet dsMerge = new DataSet(); dsMerge.Merge(ds1); dsMerge.Merge(ds2); dsMerge.Merge(ds3); DataGrid1.DataSource = dsMerge; DataGrid1.DataBind(); // Determine the total time taken. TimeSpan timeTaken = DateTime.Now.Subtract(startTime); lblInfo.Text = "Calling three methods took " + timeTaken.TotalSeconds + " seconds."; }
private void cmdAsynchronous_Click(object sender, System.EventArgs e) { // Record the start time. DateTime startTime = DateTime.Now; // Start the web service. EmployeesService proxy = new EmployeesService(); IAsyncResult handle = proxy.BeginGetEmployees(null, null); // Perform some other time-consuming tasks. DoSomethingSlow(); // Retrieve the result. If it isn't ready, wait. DataGrid1.DataSource = proxy.EndGetEmployees(handle); DataGrid1.DataBind(); // Determine the total time taken. TimeSpan timeTaken = DateTime.Now.Subtract(startTime); lblInfo.Text = "Asynchronous operations took " + timeTaken.TotalSeconds + " seconds."; }