<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
        Dim DBCon As SqlConnection
        Dim OrdersCommand As SqlCommand = New SqlCommand()
        Dim CustCommand As SqlCommand = New SqlCommand()
        Dim OrdersReader As SqlDataReader
        Dim CustReader As SqlDataReader
        Dim OrdersASyncResult As IAsyncResult
        Dim CustAsyncResult As IAsyncResult

        Dim WHIndex As Integer
        Dim WHandles(1) As Threading.WaitHandle
        Dim OrdersWHandle As Threading.WaitHandle
        Dim CustWHandle As Threading.WaitHandle

        DBCon = New SqlConnection()
        DBCon.ConnectionString = ConfigurationManager.ConnectionStrings("DSN_NorthWind").ConnectionString

        CustCommand.CommandText = "SELECT * FROM Customers WHERE CompanyName = 'Alfreds Futterkiste'"

        CustCommand.CommandType = CommandType.Text
        CustCommand.Connection = DBCon

        OrdersCommand.CommandText = 
                "SELECT Customers.CompanyName, Customers.ContactName, " & 
                "Orders.OrderID, Orders.OrderDate, " & 
                "Orders.RequiredDate, Orders.ShippedDate " & 
                "FROM Orders, Customers " & 
                "WHERE Orders.CustomerID = Customers.CustomerID " & 
                "AND Customers.CompanyName = 'Alfreds Futterkiste' " & 
                "ORDER BY Customers.CompanyName, Customers.ContactName"

        OrdersCommand.CommandType = CommandType.Text
        OrdersCommand.Connection = DBCon

        ' Otwarcie połączenia z bazą danych
        DBCon.Open ()

        ' Asynchroniczne pobranie informacji o kliencie
        CustAsyncResult = CustCommand.BeginExecuteReader()

        ' Asynchroniczne pobranie listy zamówień
        OrdersASyncResult = OrdersCommand.BeginExecuteReader()

        CustWHandle = CustAsyncResult.AsyncWaitHandle
        OrdersWHandle = OrdersASyncResult.AsyncWaitHandle

        ' Uzupełnienie tablicy uchwytów dwoma uchwytami, które
        ' zamierzamy wykorzystać w tym kodzie
        WHandles(0) = CustWHandle
        WHandles(1) = OrdersWHandle

        ' Wykonujemy całą pętlę dwa razy, bo mamy 
        ' dwa elementy w tablicy
        For Index As Integer = 0 To 1
            ' Czekamy na zakończenie wykonywania się tylko 
            ' jednego procesu asynchronicznego
            WHIndex = Threading.WaitHandle.WaitAny(WHandles)

            ' Wartością zwracaną metody WaitAny
            ' jest indeks w tablicy uchwytu, który
            ' reprezentuje zakończony proces
            Select Case WHIndex
                Case 0
                    CustReader = CustCommand.EndExecuteReader(CustAsyncResult)

                    gvCustomers.DataSource = CustReader
                    gvCustomers.DataBind()                    
                Case 1
                    OrdersReader = _
                       OrdersCommand.EndExecuteReader(OrdersASyncResult)

                    gvOrders.DataSource = OrdersReader
                    gvOrders.DataBind()

            End Select
        Next

        ' Zamykanie połączenia
        DBCon.Close()
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Technika WaitAny</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="gvCustomers" Width="100%" runat="server"></asp:GridView>
    <br /><br />
    <asp:GridView ID="gvOrders" Width="100%" AutoGenerateColumns="False"         
     runat="server">
       <Columns>
          <asp:BoundField HeaderText="Nazwa firmy" 
           DataField="CompanyName"></asp:BoundField>
          <asp:BoundField HeaderText="Kontakt" 
           DataField="ContactName"></asp:BoundField>
          <asp:BoundField HeaderText="Data zamówienia" DataField="orderdate" 
           DataFormatString="{0:d}"></asp:BoundField>
          <asp:BoundField HeaderText="Wymagana data dostawy" DataField="requireddate" 
           DataFormatString="{0:d}"></asp:BoundField>
          <asp:BoundField HeaderText="Data dostawy" DataField="shippeddate" 
           DataFormatString="{0:d}"></asp:BoundField>
       </Columns>
    </asp:GridView>
    </div>
    </form>
</body>
</html>
