关于 ASp.NET中多线程界面更新问题
ldcui 2009-03-30 06:24:10 前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px" CellPadding="4">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<div>
</div>
</form>
</body>
</html>
后台:
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;
public partial class _Default : System.Web.UI.Page
{
private Thread m_Thread;
protected void Page_Load(object sender, EventArgs e)
{
m_Thread = new Thread(new ThreadStart(Update));//后台工作线程
m_Thread.IsBackground = true;
m_Thread.Start();
}
/// <summary>
/// 目的是想在别一条线程中更新界面,但是并未成功
/// </summary>
protected void Update()
{
DataTable table = new DataTable("clientGuard");
table.Columns.Add("编号", typeof(int));
table.Columns.Add("IP", typeof(string));
table.Columns.Add("网络状态", typeof(string));
table.Columns.Add("服务器状态", typeof(string));
table.Columns.Add("服务器类型", typeof(string));
table.Columns.Add("玩家数量", typeof(string));
table.Columns.Add("作业", typeof(string));
table.Columns.Add("预警", typeof(string));
table.Columns.Add("名称", typeof(string));
table.Columns.Add("进度", typeof(string));
table.Columns.Add("GroupID", typeof(int));
table.Columns.Add("ServerID", typeof(int));
//将计算的数据放入表格中,更新界面
string strClientGuard = "00000@111111111@22222222@333333@44444444@555555555@6666666@77777@888@99@00@100@1222@1333@";
int index = 0;
string[] array = strClientGuard.Split('@');
DataRow row = table.NewRow();
row[0] = array[++index];
row[1] = array[++index];
row[2] = array[++index];
row[3] = array[++index];
row[4] = array[++index];
row[5] = array[++index];
row[6] = array[++index];
row[7] = array[++index];
row[8] = array[++index];
row[9] = array[++index];
row[10] = array[++index];
row[11] = array[++index];
table.Rows.Add(row);
DataSet ds = new DataSet();
ds.Tables.Add(table);
this.GridView1.AutoGenerateColumns = true;
this.GridView1.DataSource = ds;
this.GridView1.DataMember = "clientGuard";
this.GridView1.DataBind();
this.UpdatePanel1.Update();//利用ajax的UpdatePanel中的Update方法进行更新
}
}
请教一下如何才能在界面上显示出表格中的数据,以上代码不能正确地显示出内容