在不同的线程HttpContext.Current.Items [英] HttpContext.Current.Items in different thread
问题描述
使用HttpContext.Current.Items我们可以访问从当前请求的变量
我的问题是什么,如果请求移动到不同的线程,我们还可以访问此?
如果是的,我们怎么能访问它?
我相信它会抛出空引用异常?
我用下面的code尝试,但它抛出空参考异常
公共部分类WebForm1的:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
} 保护无效BtnClick(对象发件人,EventArgs的发送)
{
HttpContext.Current.Items [txtbox1] = txtbox1.Value;
变种T =新主题(新的Threadclas()Datamethod());
t.Start();
}
}公共类Threadclas
{
公共无效Datamethod()
{
VAR DAT = HttpContext.Current.Items [txtbox1];
** //我如何可以访问的HttpContext这里**?
}
}
可以的总是的访问 HttpContext.Current.Items
当前请求无论哪个线程ASP.Net决定运行的要求。
如果你是专门询问与异步操作的行为,在ASP.Net运行时会处理所有的线程问题透明的为您服务。有关该主题的更多信息,我建议
的http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4
Using HttpContext.Current.Items we can access variables from current request
My question is what if the request moves to different thread, can we still access this ?
if Yes, how can we access it ?
I assume it will throw null reference exception ?
I am trying with the below code, but it throws Null Ref Exception
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnClick(object sender, EventArgs e)
{
HttpContext.Current.Items["txtbox1"] = txtbox1.Value;
var t = new Thread(new Threadclas().Datamethod());
t.Start();
}
}
public class Threadclas
{
public void Datamethod()
{
var dat = HttpContext.Current.Items["txtbox1"];
**//how can i access HttpContext here** ?
}
}
You can always access HttpContext.Current.Items
from the current request, no matter which thread ASP.Net decides to run the request on.
If you're specifically asking about behavior with async actions, the ASP.Net runtime will handle all threading issues transparently for you. For more information on that topic, I suggest
http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4
这篇关于在不同的线程HttpContext.Current.Items的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!