Mappath cannot be created in the thread, mainly because: httpcontext. Current is null, so:
The key is to construct an httpcontext
Public class csiteconfig
{
Private Static httpcontext Ct = new httpcontext (New httprequest ("", "http://dewrqiwe5555rqwekxczvm23482734zxcadfasdf.com", ""), new httpresponse (new system. Io. stringwriter ()));
//... Omitted
Public static string mappath (string strpath)
{
Return CT. server. mappath (strpath );
}
}
Test:
Public class processthread
{
Protected processthread ()
{
_ Sb. appendline (datetime. Now. tostring ("HH: mm: Ss. fff") + "sqlprocessthread start. <br/> ");
_ Sb. appendline ("mappath: Server. mappath (/News)" + httpcontext. Current. server. mappath ("/News") + "<br/> ");
Int nthreadcount = 0;
For (nthreadcount = 1; nthreadcount <nmaxthread + 1; nthreadcount ++)
{
Thread t = new thread (New threadstart (sqlworkthread ));
T. Name = "sqlthread No." + nthreadcount. tostring ();
T. Start ();
}
_ Sb. appendline (datetime. Now. tostring ("HH: mm: Ss. fff") + ". OK. <br/> ");
}
Public static processthread instance ()
{
If (_ this = NULL)
_ This = new processthread ();
Return _ this;
}
Public void sqlworkthread ()
{// Thread
_ Sb. appendline (thread. currentthread. Name + "starting... <br> ");
Try {
_ Sb. appendline (thread. currentthread. Name + "mappath: csiteconfig. instance (). mappath (/News)" +Csiteconfig. mappath ("/html ")+ "<Br/> ");// If httpcontext. Current. server. mappath is used here, an error is guaranteed!
}
Catch (exception ex)
{
_ Sb. appendline (ex. Message + "<br/> ");
_ Sb. appendline (ex. Source + "<br/> ");
_ Sb. appendline (ex. targetsite + "<br/> ");
}
}
Public String getlog
{
Get
{
Return _ sb. tostring () + "<br> busythread count:" + _ ntmpbusythread. tostring () + "<br> worktimes:" + _ ulworktimes. tostring ();
}
}
}
The ASPX page is omitted:
Label1.text = "mappath: csiteconfig. mappath (/html) "+ leescms. common. csiteconfig. mappath ("/html") + "<br/>" + leescms. common. processthread. instance (). getlog;