C # using system. Threading. thread. Sleep for immediate output

Source: Internet
Author: User

A website must generate a static page.
According to past experience, all the operations that consume a lot of energy in a short period of time will show "Service unavailable", so that the webpage cannot be opened normally.
To generate a static page, you need to read a large amount of data in a short period of time and save it as an HTML page. If some space runs well, you will be afraid of the junk space ......
When generating a message, you must immediately output the message to notify the customer.

After thinking based on experience, there are two feasible methods:
1. generate only one HTML page at a time, and then output the information to the customer. For example, "the homepage has been generated and the news page is being generated. Please wait ...".CodePlace the JS Code in. The JS Code can refresh the generated page every two seconds to generate the next HTML page ...... Until the process is completed.

ThisAlgorithmComplex because the pages to be generated are not of a uniform type, such as homepage, news page, and message page ...... And the steps to generate a record at a time involve many problems.
2. Use System. Threading. thread. sleep to generate HTML pages intermittently without causing too much data consumption in a short period of time. However, if the thread is still consuming resources when it is suspended, this method is unavailable.

After testing, when system. Threading. thread. Sleep is suspended, it does not consume CPU or memory. You can use method 2 to generate HTML code. And no other settings are required. For Loop 1000 times, system. threading. thread. when sleep is set to 2 seconds, it does not time out, that is, the cycle is 1000 times. The interval between each cycle is paused for 2 seconds. After the execution is completed, it takes 2*1000 seconds and does not time out.
BriefProgramCode:
Copy codeThe Code is as follows: // notify the customer first and generate
Response. Write ("generating file ...");
Response. Flush ();
// Generate HTML pages through the for loop. This example is used only.
For (INT I = 0; I <= 1000; I ++)
{
// Generate HTML code
// Notification
Response. Write ("XXX page generated successfully. Please wait for the next page to generate ...");
Response. Flush ();

// Suspend for 2 seconds, and perform the next loop after 2 seconds to generate the next page
System. Threading. thread. Sleep (2*1000 );
}
Response. Write ("All pages have been generated ");

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.