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 cmdGetEmployees_Click(object sender, System.EventArgs e) { // Disable the button so that only one asynchronous // call will be permitted at a time. cmdGetEmployees.Enabled = false; // Create the proxy. EmployeesService proxy = new EmployeesService(); // Create the callback delegate. AsyncCallback callback = new AsyncCallback(Callback); // Call the web service asynchronously and // pass the callback and the proxy object. // There is no need to store the IAsyncResult object, // because this example doesn't check the state. proxy.BeginGetEmployees(callback, proxy); }
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."; }