关于 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方法进行更新
}
}
请教一下如何才能在界面上显示出表格中的数据,以上代码不能正确地显示出内容
...全文
291 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
itcrazyman 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ojekleen 的回复:]
不明白楼主是什么意思?放弃主线程不用起个子线程?而且你起的线程又有啥优势?
如果ajax访问量很大的话,这样创建线程也是很痛苦的事情
这种情况是根本没必要起子线程的,
[/Quote]
ojekleen 2009-03-31
  • 打赏
  • 举报
回复
不明白楼主是什么意思?放弃主线程不用起个子线程?而且你起的线程又有啥优势?
如果ajax访问量很大的话,这样创建线程也是很痛苦的事情
这种情况是根本没必要起子线程的,


linaren 2009-03-31
  • 打赏
  • 举报
回复
问题可能在于执行完你这个类的pageload后ajax的后台响应已经结束了,

this.UpdatePanel1.Update();这行代码就没机会返回给前台了。
你可以不用线程如果可以的话,肯定是这个原因了,

如果是这个原因,你可以试下在m_Thread.Start();后加上一段代码等待线程执行完,不过这样的话用线程估计没什么意义了

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧