--- title: "ASP.NET applications using wait handles" description: "Provides an example demonstrating how to execute multiple concurrent commands from an ASP.NET page, using Wait handles to manage the operation at completion of all the commands." ms.date: "09/30/2019" dev_langs: - "csharp" ms.assetid: f588597a-49de-4206-8463-4ef377e112ff ms.prod: sql ms.prod_service: connectivity ms.technology: connectivity ms.topic: conceptual author: David-Engel ms.author: v-daenge ms.reviewer: v-kaywon --- # ASP.NET applications using wait handles [!INCLUDE[Driver_ADONET_Download](../../../includes/driver_adonet_download.md)] The callback and polling models for handling asynchronous operations are useful when your application is processing only one asynchronous operation at a time. The Wait models provide a more flexible way of processing multiple asynchronous operations. There are two Wait models, named for the methods used to implement them: the Wait (Any) model and the Wait (All) model. To use either Wait model, you need to use the property of the object returned by the , , or methods. The and methods both require you to send the objects as an argument, grouped together in an array. Both Wait methods monitor the asynchronous operations, waiting for completion. The method waits for any of the operations to complete or time out. Once you know a particular operation is complete, you can process its results and then continue waiting for the next operation to complete or time out. The method waits for all of the processes in the array of instances to complete or time out before continuing. The Wait models' benefit is most striking when you need to run multiple operations of some length on different servers, or when your server is powerful enough to process all the queries at the same time. In the examples presented here, three queries emulate long processes by adding WAITFOR commands of varying lengths to inconsequential SELECT queries. ## Example: Wait (Any) model The following example illustrates the Wait (Any) model. Once three asynchronous processes are started, the method is called to wait for the completion of any one of them. As each process completes, the method is called and the resulting object is read. At this point, a real-world application would likely use the to populate a portion of the page. In this simple example, the time the process completed is added to a text box corresponding to the process. Taken together, the times in the text boxes illustrate the point: Code is executed each time a process completes. To set up this example, create a new ASP.NET Web Site project. Place a control and four controls on the page (accepting the default name for each control). Add the following code to the form's class, modifying the connection string as necessary for your environment. ```csharp // Add the following using statements, if they are not already there. using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Threading; using Microsoft.Data.SqlClient; // Add this code to the page's class string GetConnectionString() // To avoid storing the connection string in your code, // you can retrieve it from a configuration file. // If you have not included "Asynchronous Processing=true" // in the connection string, the command will not be able // to execute asynchronously. { return "Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=AdventureWorks;" + "Asynchronous Processing=true"; } void Button1_Click(object sender, System.EventArgs e) { // In a real-world application, you might be connecting to // three different servers or databases. For the example, // we connect to only one. SqlConnection connection1 = new SqlConnection(GetConnectionString()); SqlConnection connection2 = new SqlConnection(GetConnectionString()); SqlConnection connection3 = new SqlConnection(GetConnectionString()); // To keep the example simple, all three asynchronous // processes select a row from the same table. WAITFOR // commands are used to emulate long-running processes // that complete after different periods of time. string commandText1 = "WAITFOR DELAY '0:0:01';" + "SELECT * FROM Production.Product " + "WHERE ProductNumber = 'BL-2036'"; string commandText2 = "WAITFOR DELAY '0:0:05';" + "SELECT * FROM Production.Product " + "WHERE ProductNumber = 'BL-2036'"; string commandText3 = "WAITFOR DELAY '0:0:10';" + "SELECT * FROM Production.Product " + "WHERE ProductNumber = 'BL-2036'"; try // For each process, open a connection and begin // execution. Use the IAsyncResult object returned by // BeginExecuteReader to add a WaitHandle for the // process to the array. { connection1.Open(); SqlCommand command1 = new SqlCommand(commandText1, connection1); IAsyncResult result1 = command1.BeginExecuteReader(); WaitHandle waitHandle1 = result1.AsyncWaitHandle; connection2.Open(); SqlCommand command2 = new SqlCommand(commandText2, connection2); IAsyncResult result2 = command2.BeginExecuteReader(); WaitHandle waitHandle2 = result2.AsyncWaitHandle; connection3.Open(); SqlCommand command3 = new SqlCommand(commandText3, connection3); IAsyncResult result3 = command3.BeginExecuteReader(); WaitHandle waitHandle3 = result3.AsyncWaitHandle; WaitHandle[] waitHandles = { waitHandle1, waitHandle2, waitHandle3 }; int index; for (int countWaits = 0; countWaits <= 2; countWaits++) { // WaitAny waits for any of the processes to // complete. The return value is either the index // of the array element whose process just // completed, or the WaitTimeout value. index = WaitHandle.WaitAny(waitHandles, 60000, false); // This example doesn't actually do anything with // the data returned by the processes, but the // code opens readers for each just to demonstrate // the concept. // Instead of using the returned data to fill the // controls on the page, the example adds the time // the process was completed to the corresponding // text box. switch (index) { case 0: SqlDataReader reader1; reader1 = command1.EndExecuteReader(result1); if (reader1.Read()) { TextBox1.Text = "Completed " + System.DateTime.Now.ToLongTimeString(); } reader1.Close(); break; case 1: SqlDataReader reader2; reader2 = command2.EndExecuteReader(result2); if (reader2.Read()) { TextBox2.Text = "Completed " + System.DateTime.Now.ToLongTimeString(); } reader2.Close(); break; case 2: SqlDataReader reader3; reader3 = command3.EndExecuteReader(result3); if (reader3.Read()) { TextBox3.Text = "Completed " + System.DateTime.Now.ToLongTimeString(); } reader3.Close(); break; case WaitHandle.WaitTimeout: throw new Exception("Timeout"); break; } } } catch (Exception ex) { TextBox4.Text = ex.ToString(); } connection1.Close(); connection2.Close(); connection3.Close(); } ``` ## Example: Wait (All) model The following example illustrates the Wait (All) model. Once three asynchronous processes are started, the method is called to wait for the processes to complete or time out. Like the example of the Wait (Any) model, the time the process completed is added to a text box corresponding to the process. Again, the times in the text boxes illustrate the point: Code following the method is executed only after all processes are complete. To set up this example, create a new ASP.NET Web Site project. Place a control and four controls on the page (accepting the default name for each control). Add the following code to the form's class, modifying the connection string as necessary for your environment. ```csharp // Add the following using statements, if they are not already there. using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Threading; using Microsoft.Data.SqlClient; // Add this code to the page's class string GetConnectionString() // To avoid storing the connection string in your code, // you can retrieve it from a configuration file. // If you have not included "Asynchronous Processing=true" // in the connection string, the command will not be able // to execute asynchronously. { return "Data Source=(local);Integrated Security=SSPI;" + "Initial Catalog=AdventureWorks;" + "Asynchronous Processing=true"; } void Button1_Click(object sender, System.EventArgs e) { // In a real-world application, you might be connecting to // three different servers or databases. For the example, // we connect to only one. SqlConnection connection1 = new SqlConnection(GetConnectionString()); SqlConnection connection2 = new SqlConnection(GetConnectionString()); SqlConnection connection3 = new SqlConnection(GetConnectionString()); // To keep the example simple, all three asynchronous // processes execute UPDATE queries that result in // no change to the data. WAITFOR // commands are used to emulate long-running processes // that complete after different periods of time. string commandText1 = "UPDATE Production.Product " + "SET ReorderPoint = ReorderPoint + 1 " + "WHERE ReorderPoint Is Not Null;" + "WAITFOR DELAY '0:0:01';" + "UPDATE Production.Product " + "SET ReorderPoint = ReorderPoint - 1 " + "WHERE ReorderPoint Is Not Null"; string commandText2 = "UPDATE Production.Product " + "SET ReorderPoint = ReorderPoint + 1 " + "WHERE ReorderPoint Is Not Null;" + "WAITFOR DELAY '0:0:05';" + "UPDATE Production.Product " + "SET ReorderPoint = ReorderPoint - 1 " + "WHERE ReorderPoint Is Not Null"; string commandText3 = "UPDATE Production.Product " + "SET ReorderPoint = ReorderPoint + 1 " + "WHERE ReorderPoint Is Not Null;" + "WAITFOR DELAY '0:0:10';" + "UPDATE Production.Product " + "SET ReorderPoint = ReorderPoint - 1 " + "WHERE ReorderPoint Is Not Null"; try // For each process, open a connection and begin // execution. Use the IAsyncResult object returned by // BeginExecuteReader to add a WaitHandle for the // process to the array. { connection1.Open(); SqlCommand command1 = new SqlCommand(commandText1, connection1); IAsyncResult result1 = command1.BeginExecuteNonQuery(); WaitHandle waitHandle1 = result1.AsyncWaitHandle; connection2.Open(); SqlCommand command2 = new SqlCommand(commandText2, connection2); IAsyncResult result2 = command2.BeginExecuteNonQuery(); WaitHandle waitHandle2 = result2.AsyncWaitHandle; connection3.Open(); SqlCommand command3 = new SqlCommand(commandText3, connection3); IAsyncResult result3 = command3.BeginExecuteNonQuery(); WaitHandle waitHandle3 = result3.AsyncWaitHandle; WaitHandle[] waitHandles = { waitHandle1, waitHandle2, waitHandle3 }; bool result; // WaitAll waits for all of the processes to // complete. The return value is True if the processes // all completed successfully, False if any process // timed out. result = WaitHandle.WaitAll(waitHandles, 60000, false); if(result) { long rowCount1 = command1.EndExecuteNonQuery(result1); TextBox1.Text = "Completed " + System.DateTime.Now.ToLongTimeString(); long rowCount2 = command2.EndExecuteNonQuery(result2); TextBox2.Text = "Completed " + System.DateTime.Now.ToLongTimeString(); long rowCount3 = command3.EndExecuteNonQuery(result3); TextBox3.Text = "Completed " + System.DateTime.Now.ToLongTimeString(); } else { throw new Exception("Timeout"); } } catch (Exception ex) { TextBox4.Text = ex.ToString(); } connection1.Close(); connection2.Close(); connection3.Close(); } ``` ## Next steps - [Asynchronous operations](asynchronous-operations.md)