如何在线程内使用会话变量(后台进程)C#

时间:2014-03-31 13:32:15

标签: c# asp.net multithreading session caching

我在主题(后台流程)

中的会话中存储数据存在问题
 DataTable dtmultifamilyurlvalues = new DataTable();
                    dtmultifamilyurlvalues.Columns.Add("FamilyHomes");
                    dtmultifamilyurlvalues.Columns.Add("AllListiedCurrent");
                    dtmultifamilyurlvalues.Columns.Add("AllListiedTrend");
                    dtmultifamilyurlvalues.Columns.Add("BottomCurrent");
                    dtmultifamilyurlvalues.Columns.Add("BottomTrend");
                    dtmultifamilyurlvalues.Columns.Add("LowerCurrent");
                    dtmultifamilyurlvalues.Columns.Add("LowerTrend");
                    dtmultifamilyurlvalues.Columns.Add("UpperCurrent");
                    dtmultifamilyurlvalues.Columns.Add("UpperTrend");
                    dtmultifamilyurlvalues.Columns.Add("TopCurrent");
                    dtmultifamilyurlvalues.Columns.Add("TopTrend");

                    //House Size
                    dtmultifamilyurlvalues.Rows.Add("Median House Size", Getmedian(familtypevalue, "a", "Sqft:MEDIAN"), GetTrends(familtypevalue, "a", "Sqft:MEDIAN_trend") + " " + GetTrends(familtypevalue, "a", "Sqft:MEDIAN_trend_strength"),
                        Getmedian(familtypevalue, "b", "Sqft:MEDIAN"), GetTrends(familtypevalue, "b", "Sqft:MEDIAN_trend") + " " + GetTrends(familtypevalue, "b", "Sqft:MEDIAN_trend_strength"),
                        Getmedian(familtypevalue, "l", "Sqft:MEDIAN"), GetTrends(familtypevalue, "l", "Sqft:MEDIAN_trend") + " " + GetTrends(familtypevalue, "l", "Sqft:MEDIAN_trend_strength"),
                        Getmedian(familtypevalue, "u", "Sqft:MEDIAN"), GetTrends(familtypevalue, "u", "Sqft:MEDIAN_trend") + " " + GetTrends(familtypevalue, "u", "Sqft:MEDIAN_trend_strength"),
                        Getmedian(familtypevalue, "t", "Sqft:MEDIAN"), GetTrends(familtypevalue, "t", "Sqft:MEDIAN_trend") + " " + GetTrends(familtypevalue, "t", "Sqft:MEDIAN_trend_strength"));


 HttpContext.Current.Session["datatablevalues"] = dtmultifamilyurlvalues;
 //HttpRuntime.Cache.Insert("multifamilyAltos", dtmultifamilyurlvalues,null,System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 60), System.Web.Caching.CacheItemPriority.Default,null);
                });
            Multifamily.IsBackground = true;
            Multifamily.Start();

这是通过错误对象引用错误,

我也使用过HttpRuntime.Cache,但是当我在两个系统中运行它产生相同的结果时,它应该根据输入而不同(我认为它会覆盖该值)

我搜索到网络并知道httpcontext无法在线程内工作。

所以,请告诉我这个问题的解决方案。

1 个答案:

答案 0 :(得分:0)

var data= SynchronizationContext.Current;


Task t = Task.Run(() => {

    string sessionValue = null;

    data.Post(() => {

        sessionValue = HttpContext.Current.Session["value"];
    }


});