다음을 통해 공유


대기 핸들을 사용한 ASP.NET 응용 프로그램(ADO.NET)

업데이트: November 2007

응용 프로그램에서 비동기 작업을 한 번에 하나씩만 처리할 경우 비동기 작업 처리를 위한 콜백 및 폴링 모델이 유용합니다. Wait 모델을 사용하면 여러 개의 비동기 작업을 보다 유연한 방식으로 처리할 수 있습니다. Wait 모델에는 Wait(Any) 모델과 Wait(All) 모델의 두 가지가 있으며 이를 구현하는 데 사용되는 WaitHandle 메서드에 대해 이름이 지정됩니다.

어떤 Wait 모델을 사용하든 BeginExecuteNonQuery, BeginExecuteReader 또는 BeginExecuteXmlReader 메서드에 의해 반환되는 IAsyncResult 개체의 AsyncWaitHandle 속성을 사용해야 합니다. WaitAnyWaitAll 메서드에서는 WaitHandle 개체를 배열에 그룹화하여 인수로 보내야 합니다.

두 Wait 메서드 모두 비동기 작업을 모니터링하며 완료될 때까지 기다립니다. WaitAny 메서드는 작업이 완료되거나 시간이 초과되기를 기다립니다. 특정 작업이 완료되면 해당 결과를 처리한 다음 계속해서 다음 작업이 완료되거나 시간이 초과되기를 기다릴 수 있습니다. WaitAll 메서드는 WaitHandle 인스턴스 배열의 모든 프로세스가 완료되거나 시간이 초과되기를 기다린 후 계속해서 프로세스를 진행합니다.

Wait 모델의 이점은 서로 다른 서버에서 일정한 길이의 여러 작업을 실행해야 하거나 서버의 성능이 모든 쿼리를 동시에 처리할 수 있을 만큼 강력한 경우에 가장 두드러지게 나타납니다. 여기에 소개된 예제에서 세 개의 쿼리가 다양한 길이의 WAITFOR 명령을 중요하지 않은 SELECT 쿼리에 추가하여 긴 프로세스를 에뮬레이션합니다.

예제: Wait(Any) 모델

다음 예제에서는 Wait(Any) 모델에 대해 설명합니다. 세 개의 비동기 프로세스를 시작하면 WaitAny 메서드가 호출되어 이들 중 하나가 완료되기를 기다립니다. 각 프로세스가 완료되면 EndExecuteReader 메서드가 호출되고 결과 SqlDataReader 개체를 읽습니다. 여기서 실제 응용 프로그램의 경우에는 SqlDataReader를 사용하여 페이지의 일부를 채우게 됩니다. 이 간단한 예제에서는 프로세스가 완료된 시간이 프로세스에 해당하는 텍스트 상자에 추가됩니다. 전체적으로 볼 때, 텍스트 상자의 시간은 프로세스가 완료될 때마다 코드가 실행되는 시점을 나타냅니다.

이 예제를 설정하려면 새 ASP.NET 웹 사이트 프로젝트를 만듭니다. 해당 페이지에 Button 컨트롤과 네 개의 TextBox 컨트롤의 위치를 지정합니다. 각 컨트롤의 이름은 기본 이름을 사용합니다.

사용자 환경의 필요에 따라 연결 문자열을 수정하여 다음 코드를 폼의 클래스에 추가합니다.

[Visual Basic]

' Add these to the top of the class
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Threading

' Add this code to the page's class:
    Private Function GetConnectionString() As String
        ' 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"
    End Function  

    Sub Button1_Click( _
     ByVal sender As Object, ByVal e As System.EventArgs)

        ' In a real-world application, you might be connecting to 
        '  three different servers or databases. For the example,
        '  we connect to only one.
        Dim connection1 As New SqlConnection(GetConnectionString())
        Dim connection2 As New SqlConnection(GetConnectionString())
        Dim connection3 As 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.
        Dim commandText1 As String = _
            "WAITFOR DELAY '0:0:01';" & _
            "SELECT * FROM Production.Product " & _
            "WHERE ProductNumber = 'BL-2036'"

        Dim commandText2 As String = _
            "WAITFOR DELAY '0:0:05';" & _
            "SELECT * FROM Production.Product " & _
            "WHERE ProductNumber = 'BL-2036'"

        Dim commandText3 As String = _
            "WAITFOR DELAY '0:0:10';" & _
            "SELECT * FROM Production.Product " & _
            "WHERE ProductNumber = 'BL-2036'"

        Dim waitHandles(2) As WaitHandle
        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()
            Dim command1 As New SqlCommand(commandText1, connection1)
            Dim result1 As IAsyncResult = _
             command1.BeginExecuteReader()
            waitHandles(0) = result1.AsyncWaitHandle

            connection2.Open()
            Dim command2 As New SqlCommand(commandText2, connection2)
            Dim result2 As IAsyncResult = _
             command2.BeginExecuteReader()
            waitHandles(1) = result2.AsyncWaitHandle

            connection3.Open()
            Dim command3 As New SqlCommand(commandText3, connection3)
            Dim result3 As IAsyncResult = _
             command3.BeginExecuteReader()
            waitHandles(2) = result3.AsyncWaitHandle

            Dim index As Integer
            For countWaits As Integer = 1 To 3
                ' 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.
                Select Case index
                    Case 0
                        Dim reader1 As SqlDataReader
                        reader1 = command1.EndExecuteReader(result1)
                        If reader1.Read Then
                            TextBox1.Text = _
                             "Completed " & _
                             System.DateTime.Now.ToLongTimeString()
                        End If
                        reader1.Close()

                    Case 1
                        Dim reader2 As SqlDataReader
                        reader2 = command2.EndExecuteReader(result2)
                        If reader2.Read Then
                            TextBox2.Text = _
                             "Completed " & _
                             System.DateTime.Now.ToLongTimeString()
                        End If
                        reader2.Close()
                    Case 2
                        Dim reader3 As SqlDataReader
                        reader3 = command3.EndExecuteReader(result3)
                        If reader3.Read Then
                            TextBox3.Text = _
                             "Completed " & _
                             System.DateTime.Now.ToLongTimeString()
                        End If
                        reader3.Close()
                    Case WaitHandle.WaitTimeout
                        Throw New Exception("Timeout")
                End Select

            Next
        Catch ex As Exception
            TextBox4.Text = ex.ToString
        End Try
        connection1.Close()
        connection2.Close()
        connection3.Close()

    End Sub

[C#]

// 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 System.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();
}

예제: Wait(All) 모델

다음 예제에서는 Wait(All) 모델에 대해 설명합니다. 세 개의 비동기 프로세스를 시작하면 WaitAll 메서드가 호출되어 프로세스가 완료되거나 시간이 초과되기를 기다립니다.

Wait(Any) 모델의 예제처럼 프로세스가 완료된 시간이 프로세스에 해당하는 텍스트 상자에 추가됩니다. 마찬가지로 텍스트 상자의 시간은 모든 프로세스가 완료된 후에 WaitAny 메서드 다음의 코드가 실행되는 시점을 나타냅니다.

이 예제를 설정하려면 새 ASP.NET 웹 사이트 프로젝트를 만듭니다. 해당 페이지에 Button 컨트롤과 네 개의 TextBox 컨트롤의 위치를 지정합니다. 각 컨트롤의 이름은 기본 이름을 사용합니다.

사용자 환경의 필요에 따라 연결 문자열을 수정하여 다음 코드를 폼의 클래스에 추가합니다.

[Visual Basic]

' Add these to the top of the class
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Threading

' Add this code to the page's class:
    Private Function GetConnectionString() As String
        ' 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"
    End Function  
    Sub Button1_Click( _
     ByVal sender As Object, ByVal e As System.EventArgs)

        ' In a real-world application, you might be connecting to 
        '  three different servers or databases. For the example,
        '  we connect to only one.
        Dim connection1 As New SqlConnection(GetConnectionString())
        Dim connection2 As New SqlConnection(GetConnectionString())
        Dim connection3 As 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.
        Dim commandText1 As String = _
         "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"

        Dim commandText2 As String = _
         "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"

        Dim commandText3 As String = _
         "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"

        Dim waitHandles(2) As WaitHandle

        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()
            Dim command1 As New SqlCommand(commandText1, connection1)
            Dim result1 As IAsyncResult = _
             command1.BeginExecuteNonQuery()
            waitHandles(0) = result1.AsyncWaitHandle

            connection2.Open()
            Dim command2 As New SqlCommand(commandText2, connection2)
            Dim result2 As IAsyncResult = _
             command2.BeginExecuteNonQuery()
            waitHandles(1) = result2.AsyncWaitHandle

            connection3.Open()
            Dim command3 As New SqlCommand(commandText3, connection3)
            Dim result3 As IAsyncResult = _
             command3.BeginExecuteNonQuery()
            waitHandles(2) = result3.AsyncWaitHandle

            ' WaitAll waits for all of the processes to complete.
            ' The return value is True if all processes completed, 
            ' False if any process timed out.
            Dim result As Boolean = _
             WaitHandle.WaitAll(waitHandles, 60000, False)
            If result Then
                Dim rowCount1 As Long = _
                 command1.EndExecuteNonQuery(result1)
                TextBox1.Text = _
                 "Completed " & _
                 System.DateTime.Now.ToLongTimeString()

                Dim rowCount2 As Long = _
                 command2.EndExecuteNonQuery(result2)
                TextBox2.Text = _
                 "Completed " & _
                 System.DateTime.Now.ToLongTimeString()

                Dim rowCount3 As Long = _
                 command3.EndExecuteNonQuery(result3)
                TextBox3.Text = _
                 "Completed " & _
                 System.DateTime.Now.ToLongTimeString()
            Else
                Throw New Exception("Timeout")
            End If
        Catch ex As Exception
            TextBox4.Text = ex.ToString
        End Try
        connection1.Close()
        connection2.Close()
        connection3.Close()

    End Sub

[C#]

// 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 System.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();
}

참고 항목

기타 리소스

비동기 작업(ADO.NET)