C # Use System. Threading. Thread. Sleep to output information instantly

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, "a homepage has been generated and a news page is being generated. Please wait .. ", and then place the js Code in the output code. The js Code can refresh the generated page two seconds later to generate the next html page ...... Until the process is completed.

This algorithm is complicated because the pages to be generated are not of a uniform type. For example, some are homepage, some are news pages, and some are message pages ...... 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.

Brief program code:

// 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 ");

 

 

From: http://blog.sina.com.cn/s/blog_4384a04e0100lkpa.html

 

 

 

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.