国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

異步 HttpContext.Current實(shí)現(xiàn)取值的方法(解決異步Application,Session,Cache...等失效的問(wèn)題)

2020-01-18 01:11:23
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
回答的也多數(shù)都是:引用System.Web,不要用HttpContext.Current.Application應(yīng)該用System.Web.HttpContext.Current.Application,后來(lái)在網(wǎng)上看到一篇關(guān)于System.Runtime.Remoting.Messaging.CallContext這個(gè)類的詳細(xì)介紹才知道,原來(lái)HttpContext.Current是基于System.Runtime.Remoting.Messaging.CallContext這個(gè)類,子線程和異步線程都無(wú)法訪問(wèn)到主線程在CallContext中保存的數(shù)據(jù)。所以在異步執(zhí)行的過(guò)程會(huì)就會(huì)出現(xiàn)HttpContext.Current為null的情況,為了解決子線程能夠得到主線程的HttpContext.Current數(shù)據(jù),需要在異步前面就把HttpContext.Current用HttpContext的方式存起來(lái),然后能過(guò)參數(shù)的形式傳遞進(jìn)去,下面看看實(shí)現(xiàn)的方法:
復(fù)制代碼 代碼如下:

public HttpContext context
{
get { return HttpContext.Current; }
set { value = context; }
}

然后建立一個(gè)委托
復(fù)制代碼 代碼如下:

public delegate string delegategetResult(HttpContext context);

下面就是實(shí)現(xiàn)過(guò)程的編碼
復(fù)制代碼 代碼如下:

protected void Page_Load(object sender, EventArgs e)
{
context = HttpContext.Current;
delegategetResult dgt = testAsync;
IAsyncResult iar = dgt.BeginInvoke(context, null, null);
string result = dgt.EndInvoke(iar);
Response.Write(result);
}

public static string testAsync(HttpContext context)
{
if (context.Application["boolTTS"] == null)
{
Hashtable ht = (Hashtable)context.Application["TTS"];
if (ht == null)
{
ht = new Hashtable();
}

if (ht["A"] == null)
{
ht.Add("A", "A");
}

if (ht["B"] == null)
{
ht.Add("B", "B");
}

context.Application["TTS"] = ht;
}

Hashtable hts = new Hashtable();
hts = (Hashtable)context.Application["TTS"];
if (hts["A"] != null)
{
return "恭喜,中大獎(jiǎng)呀";
}
else
{
return "我猜你快中獎(jiǎng)了";
}
}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 海晏县| 益阳市| 彭州市| 油尖旺区| 泗洪县| 孝昌县| 武夷山市| 克什克腾旗| 龙川县| 青海省| 固原市| 沙湾县| 永寿县| 互助| 迭部县| 江北区| 通江县| 玉环县| 大姚县| 佳木斯市| 呼和浩特市| 连城县| 青铜峡市| 襄汾县| 宣化县| 靖边县| 新泰市| 天气| 巫溪县| 喜德县| 凉山| 察雅县| 昌都县| 百色市| 雷波县| 正蓝旗| 大竹县| 大兴区| 砀山县| 江门市| 平昌县|