.net 多线程下HttpContext.Current 的问题

因为一个系统的查询要做一个超时提示的功能,想到用线程来做,结果用了线程后之前正常运行的系统出错了。

跟踪到出错的位置发现HttpContext.Current 为空,产生“未将对象引用到对象实例”的异常。搜索线程 HttpContext发现已经有人写了个方法可以解决问题,直接拿来用了。方法如下

public static string MapPath(string strPath)   
{   
    if (System.Web.HttpContext.Current != null)   
    {   
        return System.Web.HttpContext.Current.Server.MapPath(strPath);   
    }   
    else //非web程序引用    
    {   
        strPath = strPath.Replace("/", "");   
        strPath = strPath.Replace("~","");   
        if (strPath.StartsWith("//"))   
        {   
            strPath = strPath.TrimStart('//');   
        }   
        return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath)+"\\";   
    }   
}  


你可能感兴趣的:(多线程,.net)