Does closing ASP. NET midway through affect server execution? (1)

Source: Internet
Author: User

Execute ASP. NET page process, it is inevitable that such a problem will occur, a typical problem is that when An ASPX page is half executed, the browser closes this page, so is the code for the page corresponding to the server still being executed?

Experience has proved that the code is still being executed unless you make a special judgment in the code.

Note:

1. When the client displays the page, the page objects that have been executed in the background no longer exist. Of course, at this time, the server segment cannot be executed.

2. The page has not been returned, and is in the waiting status. When the ASPX page is closed, the above mentioned server is still running.

3. When the client is disabled, no command is sent to the server.

4. Unless a special judgment is made in your code, the special judgment here refers

 
 
  1. if(!Response.IsClientConnected) 

To detect the status and use the code to terminate the operation.

The following simple code demonstrates whether the page is still being executed after the page is closed?

After opening the page, you can close the page when no information is returned, and then check whether the corresponding file in the specified directory is created and filled in.

 
 
  1. Protected VoidPage_Load (ObjectSender, EventArgs e)
  2. {
  3. StringBuilder txt =NewStringBuilder ();
  4. Txt. AppendLine ();
  5. Txt. AppendLine (DateTime. Now. ToString ("U"));
  6. Txt. AppendLine ("Asvd");
  7. Response. Write (DateTime. Now. ToString ("U"));
  8. Response. Write (" \ R \ n");
  9. Thread. Sleep (50000 );
  10.  
  11. Txt. AppendLine (DateTime. Now. ToString ("U"));
  12. Response. Write (DateTime. Now. ToString ("U"));
  13. Response. Write (" \ R \ n");
  14. // Write some information to another file to check whether the file is running 
  15. StringDir = Path. Combine (AppDomain. CurrentDomain. BaseDirectory,"Logs");
  16. If(! Directory. Exists (dir ))
  17. Directory. CreateDirectory (dir );
  18. DateTime dt = DateTime. Now;
  19. StringRequired filename =String. Format ("Errors _ {}. log", Dt. Year, dt. Month, dt. Day );
  20. StringFileName = Path. Combine (dir, delimiter fileName );
  21. StreamWriter sw;
  22. If(File. Exists (fileName ))
  23. Sw = File. AppendText (fileName );
  24. Else
  25. Sw = File. CreateText (fileName );
  26. Sw. Write (txt. ToString ());
  27. Sw. Close ();
  28. Sw =Null;
  29. }

A simple example of a special judgment is given:

Note: The IsClientConnected judgment is not supported on the Development site ASP. NET Development Server that comes with the VS.net Development tool. ASP. NET Development Server returns true forever.
IIS is supported.

 
 
  1. Protected VoidPage_Load (ObjectSender, EventArgs e)
  2. {
  3. StringBuilder txt =NewStringBuilder ();
  4. For(IntI = 0; I <100; I ++)
  5. {
  6. If(This. Response. IsClientConnected)
  7. {
  8. Txt. AppendLine ();
  9. Txt. AppendLine (DateTime. Now. ToString ("U"));
  10. Txt. AppendLine (I. ToString ());
  11. Response. Write (DateTime. Now. ToString ("U"));
  12. Response. Write (" \ R \ n");
  13. Thread. Sleep (500 );
  14. }
  15. Else
  16. {
  17. Response. End ();
  18. Return;
  19. }
  20. }
  21. Txt. AppendLine (DateTime. Now. ToString ("U"));
  22. Response. Write (DateTime. Now. ToString ("U"));
  23. Response. Write (" \ R \ n");
  24. // Write some information to another file to check whether the file is running 
  25. StringDir = Path. Combine (AppDomain. CurrentDomain. BaseDirectory,"Logs");
  26. If(! Directory. Exists (dir ))
  27. Directory. CreateDirectory (dir );
  28. DateTime dt = DateTime. Now;
  29. StringRequired filename =String. Format ("Errors _ {}. log", Dt. Year, dt. Month, dt. Day );
  30. StringFileName = Path. Combine (dir, delimiter fileName );
  31. StreamWriter sw;
  32. If(File. Exists (fileName ))
  33. Sw = File. AppendText (fileName );
  34. Else
  35. Sw = File. CreateText (fileName );
  36. Sw. Write (txt. ToString ());
  37. Sw. Close ();
  38. Sw =Null;
  39. }

In this example, when an interruption is detected, nothing previously done is discarded.


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.