C USES System.Threading.Thread.Sleep to provide a detailed explanation of the immediate output information

  • 2020-05-12 03:08:43
  • OfStack

There is a website that needs to generate static pages.
According to past experience, "service unavailable" will appear in any poor space, and it will not be able to open the web page normally, if the energy-intensive computation is run in a short period of time.
Generate static page, need to read a lot of data in a short time and save as html page, better space to run no problem, afraid of those garbage space......
At the time of generation, the client needs to be notified of the instant output information.

Based on empirical thinking, there are two possible approaches:
1. Only generate one html page at a time, and then output information to the customer, such as "home page has been generated, news page is being generated, please wait a moment.." , and then place the js code in the output code. The js code can refresh the generated page again every 2 seconds to execute the next generation of html page... Until the end.

This algorithm is more complex, because the page to be generated is not the same as 1 type, such as some home page, some news page, some message page... And there are many problems involved in which step to generate records one by one.
. 2, use System Threading. Thread. Sleep if hangs, the generation of intermittent html pages, can cause no short time internal friction is too big. However, if the thread is still consuming resources when it is suspended, this method is not available.

Tested, System. Threading. Thread. Sleep hang, do not consume cpu and memory. You can generate html code using method 2. And does not require any other Settings, for loop 1000 times, System. Threading. Thread. Sleep set to 2 seconds, not timeout, namely loop 1000 times, suspended between 2 seconds of each loop, performed need: 2 * 1000 seconds, not timeout.
Brief program code:

// First notify the customer that it is being generated 
Response.Write(" Please wait while the file is being generated ...");
Response.Flush();
// through for Generate each cycle html Page, for example only 
for (int i = 0; i <= 1000; i++)
{
   // generate html The code is slightly 
   // notice 
   Response.Write("XXX Page generated successfully, please wait 1 Page generation ...");
   Response.Flush();

   // hang 2 Second, 2 Let's do it in seconds 1 A loop to generate the next 1 page 
   System.Threading.Thread.Sleep(2 * 1000);
}
Response.Write(" All pages are generated ");


Related articles: