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

首頁 > 編程 > .NET > 正文

異步 HttpContext.Current實現(xiàn)取值的方法(解決異步Application,Session,C

2024-07-10 12:38:39
字體:
供稿:網(wǎng)友
回答的也多數(shù)都是:引用System.Web,不要用HttpContext.Current.Application應該用System.Web.HttpContext.Current.Application,后來在網(wǎng)上看到一篇關于System.Runtime.Remoting.Messaging.CallContext這個類的詳細介紹才知道,原來HttpContext.Current是基于System.Runtime.Remoting.Messaging.CallContext這個類,子線程和異步線程都無法訪問到主線程在CallContext中保存的數(shù)據(jù)。所以在異步執(zhí)行的過程會就會出現(xiàn)HttpContext.Current為null的情況,為了解決子線程能夠得到主線程的HttpContext.Current數(shù)據(jù),需要在異步前面就把HttpContext.Current用HttpContext的方式存起來,然后能過參數(shù)的形式傳遞進去,下面看看實現(xiàn)的方法:
代碼如下:
public HttpContext context
{
get { return HttpContext.Current; }
set { value = context; }
}

然后建立一個委托
代碼如下:
public delegate string delegategetResult(HttpContext context);

下面就是實現(xiàn)過程的編碼
代碼如下:
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 "恭喜,中大獎呀";
}
else
{
return "我猜你快中獎了";
}
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 江津市| 隆回县| 明光市| 奉贤区| 永泰县| 晋州市| 辉县市| 景谷| 泰兴市| 洪洞县| 定南县| 漳州市| 孟村| 长岛县| 余姚市| 安徽省| 专栏| 昌图县| 尼玛县| 呼伦贝尔市| 抚宁县| 介休市| 射阳县| 乐昌市| 清丰县| 贵定县| 兖州市| 平乡县| 屏边| 黔江区| 招远市| 余干县| 兴海县| 晋宁县| 会理县| 石屏县| 镇江市| 武宣县| 若羌县| 武定县| 巨野县|