.net4.0 listview与task配合实现异步查询的功能
本帖最后由 bug810 于 2013-08-02 10:10:30 编辑 listview代码如下:
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="itemPlaceholder"
onpagepropertieschanging="ListView1_PagePropertiesChanging">
<LayoutTemplate>
<table style="width:100%;" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#FFFFFF" id="tblCategories">
<thead><tr style="background:#E3E3E3; font-weight:bold; text-align:center; height:25px;">
<td>
货物追踪</td>
</tr>
</thead>
<tbody>
<asp:PlaceHolder runat="server" ID="ItemPlaceHolder"></asp:PlaceHolder>
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td colspan="10">
<%# 这里显示查询结果 %>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
[TestMethod]
public void TestMethod1()
{
//
// TODO: 在此添加测试逻辑
//
var task1 = new Task<TreadObj>(getResult, new TreadObj("type1", "1"));
task1.Start();
var task2 = new Task<TreadObj>(getResult, new TreadObj("type2", "2"));
task2.Start();
var task3 = new Task<TreadObj>(getResult, new TreadObj("type3", "3"));
task3.Start();
var task4 = new Task<TreadObj>(getResult, new TreadObj("type4", "4"));
task4.Start();
var task5 = new Task<TreadObj>(getResult, new TreadObj("type5", "5"));
task5.Start();
var task6 = new Task<TreadObj>(getResult, new TreadObj("type6", "6"));
task6.Start();
Task.WaitAll(task1, task2, task3, task4, task5, task6);
Console.Write(string.Format("results:{0}", task1.Result.result
+ task2.Result.result
+ task3.Result.result
+ task4.Result.result
+ task5.Result.result
+ task6.Result.result
));
}
public class TreadObj
{
public string typeCom;
public string nu;
public string result;
public TreadObj(string _typeCom,string _nu)
{
typeCom = _typeCom;
nu = _nu;
}
}
public static TreadObj getResult(object obj)
{
TreadObj hobj = (TreadObj)obj;
hobj.result = chahuo(hobj.typeCom, hobj.nu);
return hobj;
}
public static string chahuo(string typeCom, string nu)
{
return typeCom+nu;
}