|zyciis| 为什么我在多线程中的Session赋的值只有第一次有效而后面再对他赋的值就没有效果呢!

zyciis320 2009-04-29 04:39:49

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="web.WebForm5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:UpdatePanel ID="upList" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Timer ID="tmUpdate" runat="server" Enabled="false" OnTick="tmUpdate_Tick" Interval="2000" />
</ContentTemplate>
</asp:UpdatePanel>
<div>
</div>
</form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace web
{
public partial class WebForm5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void tmUpdate_Tick(object sender, EventArgs e)
{
if (Session[Session.SessionID] != null)
{
TextBox1.Text = Session[Session.SessionID].ToString();
if (TextBox1.Text == "100")
{
Session.Remove(Session.SessionID);
tmUpdate.Enabled = false;
}
}

}

protected void Button1_Click(object sender, EventArgs e)
{
System.Threading.Thread NewTh = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Update));
//System.Threading.Thread NewTh = new System.Threading.Thread(new System.Threading.ThreadStart(Update));
NewTh.SetApartmentState(System.Threading.ApartmentState.STA);
NewTh.Start(Session);
tmUpdate.Enabled = true;
//while (NewTh.ThreadState == System.Threading.ThreadState.Running)
//{
//}
}

void Update(Object _Session)
{
//System.Web.HttpContext.Current.Session["1"] = DateTime.Now;
System.Threading.Thread.Sleep(3000);
System.Web.SessionState.HttpSessionState session = (System.Web.SessionState.HttpSessionState)_Session;
System.Threading.Thread.Sleep(3000);
session[session.SessionID] = "10";
System.Threading.Thread.Sleep(3000);
Session[session.SessionID] = "30";
System.Threading.Thread.Sleep(3000);
Session[session.SessionID] = "50";
System.Threading.Thread.Sleep(3000);
Session[session.SessionID] = "80";
System.Threading.Thread.Sleep(3000);
Session[session.SessionID] = "100";
System.Threading.Thread.Sleep(3000);
}

}
}

上面是我的页面,我要实现的功能就是
当点击了button的时候,开一个线程去执行一段代码,那段代码的进度保存在一个Session当中
页面中的Timer每两秒去读一下现在的进度,并显示出来

但怪的是,读到的只有"10",而"30,50,80,100"都读不到,
我有点理解不了

谢谢
...全文
301 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cpp2017 2009-04-30
  • 打赏
  • 举报
回复
前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Csdn.Default"

%>

<%@ Register Assembly="ZFControls" Namespace="ZFControls" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html style="height:100%" xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<style>
*
{
font:menu;
}



.CalendarArrow
{
CURSOR: pointer;font-weight:bold;
}
</style>



<script type="text/javascript">



function showvalue()
{

div1.innerHTML= AJ.GetSessionValue().value
}

</script>

</head>

<body >

<form id="form1" runat="server">


<asp:Button ID="btnNext" runat="server" Text="确定" OnClick="Button1_Click" />


<div id="div1"></div>
</form>




</body>
</html>




cpp2017 2009-04-30
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Reflection;
using System.Xml;
using System.Data.SqlClient;

using System.Messaging;
using System.Text.RegularExpressions;
using System.Net;
using System.Data.OleDb;
namespace Csdn
{
[AjaxPro.AjaxNamespace("AJ")]
public partial class Default : System.Web.UI.Page
{
System.Web.SessionState.HttpSessionState _Session;
protected void Page_Load(object sender, EventArgs e)
{
Response.Expires = -100;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
AjaxPro.Utility.RegisterTypeForAjax(typeof(Default), this);


}


[AjaxPro.AjaxMethod]
public String GetSessionValue()
{
if (Session["a"] != null) return Session["a"].ToString();
else return "-----";
}
protected void Button1_Click(object sender, EventArgs e)
{


System.Threading.Thread NewTh = new System.Threading.Thread(new System.Threading.ThreadStart(this.Update));

NewTh.Start();


ClientScript.RegisterStartupScript(this.GetType(), "a", "window.setInterval(showvalue, 1000);", true);


}

void Update()
{

try
{

Session["a"] = "10";
System.Threading.Thread.Sleep(3000);
Session["a"] = "30";
System.Threading.Thread.Sleep(3000);
Session["a"] = "50";
System.Threading.Thread.Sleep(3000);
Session["a"] = "80";
System.Threading.Thread.Sleep(3000);
Session["a"] = "100";
System.Threading.Thread.Sleep(3000);

}
catch
{ }
}



}




}

阿非 2009-04-29
  • 打赏
  • 举报
回复

protected void Button1_Click(object sender, EventArgs e)
{
Session[Session.SessionID] = "0";
System.Threading.Thread NewTh = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Update));
//System.Threading.Thread NewTh = new System.Threading.Thread(new System.Threading.ThreadStart(Update));
NewTh.SetApartmentState(System.Threading.ApartmentState.STA);
NewTh.Start(Session);
tmUpdate.Enabled = true;
//while (NewTh.ThreadState == System.Threading.ThreadState.Running)
//{
//}
}

zyciis320 2009-04-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jingshuaizh 的回复:]
引用 1 楼 wanghao3616 的回复:

上面有sp1234大哥的回复
http://topic.csdn.net/u/20090320/11/a943fc60-5d65-417c-9483-0ad0d8c50ced.html
up
[/Quote]
---------------------------
我的并不是去更改页面控件的内容
我是去更改session的内容

谢谢
cpp2017 2009-04-29
  • 打赏
  • 举报
回复
你把Session.SessionId换成一个固定值
蓝海D鱼 2009-04-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wanghao3616 的回复:]

上面有sp1234大哥的回复
http://topic.csdn.net/u/20090320/11/a943fc60-5d65-417c-9483-0ad0d8c50ced.html
[/Quote]up
wanghao3616 2009-04-29
  • 打赏
  • 举报
回复

62,052

社区成员

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

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

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

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