Multi-thread design on ASP. NET Server -- asynchronous IHttpHandler

Source: Internet
Author: User

ThreadPool. GetAvailableThreads (out int a1, out int a2); // a1 indicates the worker thread and a2 indicates the IO thread.

Jeffrey Richter's masterpiece CLR via C #

IHttpAsyncHandler{

Public byte [] bytes;
Public HttpContext context1;

{
Throw new Exception ();
}

Context1 = context;

// 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 ",

);
Bytes = new byte [fileStream. Length];

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.

{File1.fileStream. EndRead (result );
File1.fileStream. Close ();


}

Get {
Return false;
}
}

  • 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.

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.