In the. NET system, you can directly create a Thread through the Thread. To avoid the impact of too many threads on system performance, Microsoft has developed a ThreadPool (CLR thread pool) to manage threads.
ThreadPool. QueueUserWorkItem (new WaitCallback (method), objcet); // create a thread
There are two types of threads in the CLR thread pool: worker thread and IO thread.
ThreadPool. GetAvailableThreads (out int a1, out int a2); // a1 indicates the worker thread and a2 indicates the IO thread.
Generally, ASP. NET page requests run in the worker thread. However, when using hard disk file reading and external data exchange, I/O threads can be used to reduce the stress on the worker thread. (Note: If I/O threads are used asynchronously when reading small files, the efficiency may be lower than that of single-threaded operations. We recommend that you refer to the famous "CLR via C #" by Jeffrey Richter.)
When the client needs to obtain network files, it generally uses the IHttpHandler method. If the file traffic is too large, the thread of the CLR thread pool will be occupied for a long time and in the waiting state, when the thread is greater than the maximum value of the thread pool
(The default value of IIS5.0 is 25 * Number of CPUs, IIS
7.0 the default value is 250 * The number of CPUs), other requests will be in the waiting status, this will cause pressure on the server. At this time, the asynchronous HttpHanlder can use the IO thread to process files asynchronously. The function is to release the worker thread of the CLR thread pool in time and submit the file reading work to the asynchronous IO thread.
Class File
{
FileStream fileStream;
Byte [] bytes;
}
Public class Handler:IHttpAsyncHandler
{
// Asynchronous HttpHandler inherits from IHttpAsyncHandler and includes the BeginProcessRequest method and EndProcessRequest method.
Public byte [] bytes;
Public HttpContext context1;
Public void ProcessRequest (HttpContext context)
{
Throw new Exception ();
}
Public IAsyncResult
BeginProcessRequest (HttpContext context, AsyncCallback callback, object o)
{
Context1 = context;
// When using asynchronous I/O threads, note that the following method is used to create a FileStream object and set the final parameter userAsync to true.
// If the file size is smaller than the buffer value of 1024, the file will be read at one time, and the worker thread in the CLR thread will still be used for callback.
FileStream fileStream = new FileStream ("E: // My
Webs/WebService1/TextFile.txt ",
FileMode. Open, FileAccess. Read, FileShare. Read, 1024, true
);
Bytes = new byte [fileStream. Length];
File file1 = new File ();
File1.fileStream = fileStream;
File1.bytes = bytes;
Return fileStream. BeginRead (file1.bytes, 0, (int) fileStream. Length, callback, file1 );
// At this time, the worker thread in the CLR thread pool will be released in time. When reading is completed, the IO thread will be called again to complete the operation.
}
Public void EndProcessRequest (IAsyncResult result)
{
File file1 = (File) result. AsyncState;
File1.fileStream. EndRead (result );
File1.fileStream. Close ();
Context1.Response. Write (System. Text. Encoding. UTF8.GetString (file1.bytes ));
Int n1, n2;
ThreadPool. GetAvailableThreads (out n1, out n2 );
Context1.Response. Write (String. Formate ("WorkerThreads Count is {0 }! "+
"IOThreads Count is {1 }! ", N1.ToString (), n2.ToString ()));
// Check the thread pool at this time, and the output will be WorkerThreads Count is 1000! IOThreads Count is 999!
// Indicates that the asynchronous method uses an IO thread.
}
Public bool IsReusable {
Get {
Return false;
}
}
}
You can also use Asynchronous calling when using WebService.
Http://www.pin5i.com/showtopic-10763.html
Similarly, SQL databases, sockets, and HttpWebRequest... can also be accessed asynchronously.
- DNS operations:BeginGetHostByName and BeginResolve.
- Socket operation:BeginAccept, BeginConnect, and BeginReceive.
- WebRequest operation:BeginGetRequest and BeginGetResponse.
- SqlCommand operation:BeginExecuteReader, BeginExecuteNonQuery, and so on. This may be the most common Asynchronous Operation for developing a Web application. If you need to obtain IOCP support for database operations, you need to mark Asynchronous Processing as true (false by default) in the connection string. Otherwise, an exception will be thrown when the BeginXXX operation is called.
Unfortunately, asynchronous delegation is not suitable for this method.
When asynchronous delegation is used (BeginInvoke, EndInvoke), the threads added by the system are also the worker threads in the CLR thread pool. If the asynchronous method is used numbly, it will be counterproductive, affects system performance.
ASP. NET Server multi-thread design-asynchronous Web Service
Multi-thread design on ASP. NET Server -- asynchronous IHttpHandler