asp.net - 不同线程中的 HttpContext.Current.Items

标签 asp.net

使用 HttpContext.Current.Items 我们可以访问当前请求中的变量

我的问题是,如果请求移动到不同的线程,我们仍然可以访问它吗?

如果是,我们如何访问它?

我认为它会抛出空引用异常?

我正在尝试使用下面的代码,但它抛出空引用异常

    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** ?
    }


}

最佳答案

无论 ASP.Net 决定在哪个线程上运行请求,您始终可以从当前请求访问 HttpContext.Current.Items

如果您专门询问异步操作的行为,ASP.Net 运行时将为您透明地处理所有线程问题。有关该主题的更多信息,我建议

http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4

关于asp.net - 不同线程中的 HttpContext.Current.Items,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15196556/

相关文章:

c# - LINQ to Entities 无法识别方法 'Double Parse(System.String)' 无法转换为存储表达式 C# asp.net

c# - NHibernate 树结构更新根问题

c# - 如何清除 System.Runtime.Caching.MemoryCache

asp.net - 从 aspnet_Profile 检索数据

c# - Redis 缓存 - 阻塞操作被 WSACancelBlockingCall 调用中断

asp.net - 用于将 ASP.NET MVC 6 网站部署到 Azure 的脚本

asp.net - ASP.NET 默认情况下是否利用多核处理器?

asp.net - 使用 itemtemplate 将列动态添加到 GridView

c# - 我将如何着手创建一个全局通知类?

c# - 如何使用带有字典列表的 asp.net 转发器?