用异步函数生成网页

首先请回顾一下在网页生成汇总模式的方法,参考:生成汇总模式

本节要完成同样的任务,不过改用异步函数实现,以提高负载能力。

设计步骤:

1、首先增加一个自定义函数List,这个函数除了第一行和最后一行代码,其余原样来自之前的Httrequest事件,所以准备异步函数并不复杂:

Dim e As RequestEventArgs = args(0)
'
获取要显示的页
Dim
page As Integer = 0 '默认page0,显示第一页
Dim
pageRows As Integer = 13 '每页13
If
e.GetValues.ContainsKey("page") Then  '如果地址中有page参数
    Integer.TryParse(e.GetValues("page"), page)
'
提取page参数
End
If
Dim
StartRow As Integer = page * pageRows + 1 '此页第一行
Dim
EndRow As Integer = (page + 1) * pageRows '此页最后一行
'
获取该页数据
Dim
cmd As New SQLCommand
cmd
.ConnectionName = "orders" '记得设置数据源名称
cmd
.CommandText = "Select Count(*) From {订单}"
Dim
Count As Integer = cmd.ExecuteScalar() '获取总的行数
cmd
.CommandText = "Select * From (Select Row_Number() Over(Order by 产品,客户,日期) As [NO.], 产品, 客户, 数量, 单价, 数量 * 单价 as 金额,日期 From 订单) As a "
cmd
.CommandText = cmd.CommandText & "  Where [NO.]>= " & StartRow & " And [NO.] <= " & EndRow
Dim
dt As DataTable = cmd.ExecuteReader
'根据此页数据生成网页

Dim
wb As New WeUI
With
wb.AddTable("","Table1")
    Dim nms() As String
    Dim qty As Integer
    Dim amt As Double
    .Head.AddRow("No.",
"
产品","客户","数量","单价","金额","日期")
    For i As Integer = 0 To dt.DataRows.count - 1
        Dim r As DataRow = dt.DataRows(i)
        If i > 0 Then
            Dim lr As DataRow = dt.DataRows(i-1)
            If r(
"
客户") <> lr("客户") Then
                qty = dt.compute(
"sum(
数量)","产品='" & lr("产品") & "' And 客户= '" & lr("客户") & "'")
                amt = dt.compute(
"sum(
金额)","产品='" & lr("产品") & "' And 客户= '" & lr("客户") & "'")
                With .Body.AddRow()
                    .Attribute = "style='background-color:#F0FFFF'"
                    .AddCell(
"
小计 " & lr("客户"),"colspan='3'")
                    .AddCells(qty,"",amt,"")
                End With
            End If
            If r(
"
产品") <> lr("产品") Then
                qty = dt.compute(
"sum(
数量)","产品='" & lr("产品") &  "'")
                amt = dt.compute(
"sum(
金额)","产品='" & lr("产品") & "'")
                With .Body.AddRow()
                    .Attribute = "style='background-color:#FFFFE0'"
                    .AddCell(
"
小计 " & lr("产品"),"colspan='3'")
                    .AddCells(qty,"",amt,"")
                End With
            End If
        End If
        .Body.AddRow(r("NO."),r(
"
产品"),r("客户"),r("数量"),r("单价"),r("金额"),r("日期"))
    Next
    qty = dt.compute(
"sum(
数量)")
    amt = dt.compute(
"sum(
金额)")
    If EndRow >= Count Then
        .Body.AddRow(
"
总计","","",qty,"",amt,"").Attribute = "style='background-color:#98FB98'"
    End
If

End
With
With
wb.AddButtonGroup("","btg2", False)
    If page > 0 Then
        .Add("btnPrev",
"
上一页","","List.htm?page=" & page - 1)
    End If
    If Endrow < count  Then
        .Add("btnNext",
"
下一页","","List.htm?page=" & page + 1)
    End
If

End
With
e
.WriteString(wb.Build)
e
.Handled = True '通知系统异步函数执行完毕,可以关闭信道

2、将HttpRequest事件代码改为:

Select Case e.Path
    Case "list.htm"
       
e.AsyncExecute = True 
'
通知系统,将采用异步方式生成网页
        Functions.AsyncExecute("List", e)
'
异步调用函数生成网页
End
Select

我们的异步Http服务就此搭建完成,负荷越大,负载能力提升就越明显。


本页地址:http://www.foxtable.com/mobilehelp/topics/279.htm