关于在多线程中访问session的问题(能解决给100)

mtxnyfz 2009-03-30 07:36:41
我用ajax发送一个异步请求到某个asp.net页面,这个页面开启一个多线程处理请求,但在这个线程中却不能访问session变量,如果不是用ajax发送请求则可以正常访问,向各位高手请教一下原因 ,感激不尽!!!!
...全文
1140 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jinglecat 2009-03-31
  • 打赏
  • 举报
回复
应该是能够用的,但是你得保证主线程还没被回收掉

你的代码呢 ....
mtxnyfz 2009-03-31
  • 打赏
  • 举报
回复
在线等啊
mtxnyfz 2009-03-31
  • 打赏
  • 举报
回复
aspx文件代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="e2s2_province_cxtj_test" EnableSessionState="True" %>

<!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">
<div>
<input id="text1" type="text" onchange="add()" />
<input id="Button3" type="button" style="left: 27px; position: relative; top: 223px; width: 87px;" onclick="Button3_onclick()" />

<input id="Button2" type="button" value="button" />
<input id="text2" type="text" onchange="add()" />
=
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><span id="result"></span></div>
<script type="text/javascript" >
var sign=0;




function add()
{
var a=window.document.getElementById('text1').value;
var b=window.document.getElementById('text2').value;
var a1=parseInt(a);
var b1=parseInt(b);
window.document.getElementById('result').innerHTML=e2s2_province_cxtj_test.AddTwo(a1,b1).value;

}
function getadd(rel)
{
//var k=rel.value;;
if(rel.value!=-1)
window.document.getElementById('text2').value=rel.value;
else
{ window.document.getElementById('text2').value="结束";}


}

function getadd1(rel)
{

window.document.getElementById('result').innerText=rel.value;
//send()
//e2s2_province_cxtj_test.js(getadd);
}

function xxx()
{

}

function Button3_onclick() {
//sendRequest("longrun.aspx");
e2s2_province_cxtj_test.js(xxx);

setInterval('send()',500);


}



function send(){
sign=sign+1;

e2s2_province_cxtj_test.Message(getadd);
//sendRequest("getmessage.aspx");
window.document.getElementById('text1').value=sign;

}





</script>
</form>
</body>
</html>



cs代码:


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Caching;
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;
using System.Data.SqlClient;
using System.IO;
public partial class e2s2_province_cxtj_test : System.Web.UI.Page,System.Web.SessionState.IRequiresSessionState
{

protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(e2s2_province_cxtj_test));
if (!IsPostBack)
{
Session["sign"] = 1;



}

}
[AjaxPro.AjaxMethod]
public int AddTwo(int firstint,int secondint)
{
return firstint + secondint;
}
protected void Button1_Click(object sender, EventArgs e)
{

js();
}


[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]

public int Message( )
{


//Session["sign"] = ((int)Session["sign"]) + 1;
return (int)HttpContext.Current.Session["sign"];

//if(HttpContext.Current.Application[Session["sign"].ToString()]!=null)
// return (int)HttpContext.Current.Application[Session["sign"].ToString()];
//else
// return -1;
}



[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
public void js( )
{

Thread tr1 = new Thread(new ThreadStart(js1));
tr1.Start();


}
[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
private void js1()
{
//Session["sign"] = 0;
for (int i = 0; i <= 5; i++)
{
System.Threading.Thread.Sleep(500);

Session["sign"] = (int)Session["sign"] + 1;





//Application[id] = (int)Application[id] + 1;



}



}

public void start()
{




}


}

路人乙e 2009-03-31
  • 打赏
  • 举报
回复
这样访问 HttpContext.Current.Session
小白_YY 2009-03-31
  • 打赏
  • 举报
回复
换个方法把 不用session
lfywy 2009-03-31
  • 打赏
  • 举报
回复
理论上可以,不过你用AJAX访问的页面也必须是aspx.cs文件区读SESSION
其实还有别的办法就是把session传到后台处理的页面里进行处理
dwp008 2009-03-30
  • 打赏
  • 举报
回复
State Server 和 SQL Server来存储应该是可以实现多线程功能的。因为这两种方法已经是脱离线程了。不然的话服务器负载均衡也没有办法用Session。
只是我没有仔细看过它是怎么做的,让NLB里面不同服务器上的不同线程操作到同一个数据库里面存着的Session。
wuyq11 2009-03-30
  • 打赏
  • 举报
回复
发错了,关注
http://topic.csdn.net/u/20081110/14/1D14A70C-9B2F-4277-8A7E-F9BFDDCC9C11.html
小范f-li.cn 2009-03-30
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090203/10/35F0B86B-98AB-4EC8-A81F-76E1C442A6C8.html
up
wuyq11 2009-03-30
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090203/10/35F0B86B-98AB-4EC8-A81F-76E1C442A6C8.html
mtxnyfz 2009-03-30
  • 打赏
  • 举报
回复
试过了,没用的!
Teng_s2000 2009-03-30
  • 打赏
  • 举报
回复
没有弄过,顶下吧
dwp008 2009-03-30
  • 打赏
  • 举报
回复
恩 试试看 Session不要设置为Inproc 设置为存在服务器或者数据库里面。
config文件里面可以配的

62,050

社区成员

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

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

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

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