In thread programming, the performance can be improved a lot, but we cannot use the request context because the thread is not a request! I have left an image in several places during programming, and I also hope to keep a souvenir for unknown comrades !!!
1. cache: normally. web. httpcontext. current. cache, but the httpcontext we get in the thread. current is null, so we have to use system. web. httpruntime. cache to obtain the cache instance. We can encapsulate a method so that we can call the current cache wherever it is. Code As follows: 1 Public Class Cacher
2 {
3 Private Cacher (){}
4
5 Private Static Readonly Cache;
6
7 Static Cacher (){
8 Httpcontext Context = Httpcontext. Current;
9 If (Context = Null )
10 Cache = Context. cache;
11 Else
12 Cache = Httpruntime. cache;
13 }
14 }
2. obtain the physical path of a file: we usually use system. web. httpcontext. current. request to obtain the current physical and other related paths or URLs, through the system. web. httpcontext. current. server. mappath method to obtain the physical path of the current file or directory. As in the preceding thread, this problem cannot be solved. We can use the application Program Domain (system. appdomain. currentdomain. basedirectory) to obtain the root directory. 1 Public Static String Rootpath (){
2 Return Rootpath ( " / " );
3 }
4
5 Public Static String Rootpath ( String Filepath)
6 {
7 String Rootpath = Appdomain. currentdomain. basedirectory;
8 String Separator = Path. directoryseparatorchar. tostring ();
9 Rootpath = Rootpath. Replace ( " / " , Separator );
10 If (Filepath ! = Null )
11 {
12 Filepath = Filepath. Replace ( " / " , Separator );
13 If (Filepath. Length > 0 ) && Filepath. startswith (separator )) && Rootpath. endswith (separator ))
14 {
15 Rootpath = Rootpath + Filepath. substring ( 1 );
16 }
17 Else
18 {
19 Rootpath = Rootpath + Filepath;
20 }
21 }
22 Return Rootpath;
23 }
24
25 Public String Physicalpath ( String Path)
26 {
27 Return (Rootpath (). trimend ( New Char [] {Path. directoryseparatorchar })
+ Path. directoryseparatorchar. tostring () + Path. trimstart ( New Char [] {Path. directoryseparatorchar }));
28 }
29
30 Public String Mappath ( String Path)
31 {
32 Httpcontext Context = Httpcontext. Current;
33 If (Context ! = Null )
34 {
35 Return Context. server. mappath (PATH );
36 }
37 Return Physicalpath (path. Replace ( " / " , Path. directoryseparatorchar. tostring (). Replace ( " ~ " , "" ));
38 }
OK. Now, I caught these two guys first, and I will try again later !!!