|zyciis| 为什么我在进程中的Session赋的值勤没有效果呢, 急 谢谢

zyciis320 2009-04-29 02:52:22

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)
{
//Session[Request.Url.ToString()] = DateTime.Now;

//这里是启动多线程
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.Web.SessionState.HttpSessionState session = (System.Web.SessionState.HttpSessionState)_Session;
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);
}
}
}

因为我在进程里面怎么取Session都为null
所以我就把Session传进来,
但是这样的话在线程Update中对Session进行赋值后
在TImer中根本就取不到他的值

这是怎么回事啊。。

谢谢
...全文
134 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
云想慕尘 2009-04-29
  • 打赏
  • 举报
回复
up
cpp2017 2009-04-29
  • 打赏
  • 举报
回复
不传参也是可以取到

void Update(Object _Session)
{
//System.Web.HttpContext.Current.Session["1"] = DateTime.Now;
// System.Web.SessionState.HttpSessionState session = (System.Web.SessionState.HttpSessionState)_Session;
if (Session[Session.SessionID] != null)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath("aa.txt"));
sw.Write(Session[Session.SessionID].ToString());
sw.Close();
}
cpp2017 2009-04-29
  • 打赏
  • 举报
回复
protected void Button1_Click(object sender, EventArgs e)
{
//Session[Request.Url.ToString()] = DateTime.Now;

Session[Session.SessionID] = "123214444444444";
//这里是启动多线程
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);

//while (NewTh.ThreadState == System.Threading.ThreadState.Running)
//{
//}
}

void Update(Object _Session)
{
//System.Web.HttpContext.Current.Session["1"] = DateTime.Now;
System.Web.SessionState.HttpSessionState session = (System.Web.SessionState.HttpSessionState)_Session;
if (session[session.SessionID] != null)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath("aa.txt"));
sw.Write(session[session.SessionID].ToString());
sw.Close();
}
//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);
}
cpp2017 2009-04-29
  • 打赏
  • 举报
回复
可以取到
zzxap 2009-04-29
  • 打赏
  • 举报
回复
多线程是访问不到Session的. 采用Cache解决.
itcrazyman 2009-04-29
  • 打赏
  • 举报
回复
mark up 帮顶

62,267

社区成员

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

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

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

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