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
- 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.
- Protected VoidPage_Load (ObjectSender, EventArgs e)
- {
- StringBuilder txt =NewStringBuilder ();
- Txt. AppendLine ();
- Txt. AppendLine (DateTime. Now. ToString ("U"));
- Txt. AppendLine ("Asvd");
- Response. Write (DateTime. Now. ToString ("U"));
- Response. Write (" \ R \ n");
- Thread. Sleep (50000 );
-
- Txt. AppendLine (DateTime. Now. ToString ("U"));
- Response. Write (DateTime. Now. ToString ("U"));
- Response. Write (" \ R \ n");
- // Write some information to another file to check whether the file is running
- StringDir = Path. Combine (AppDomain. CurrentDomain. BaseDirectory,"Logs");
- If(! Directory. Exists (dir ))
- Directory. CreateDirectory (dir );
- DateTime dt = DateTime. Now;
- StringRequired filename =String. Format ("Errors _ {}. log", Dt. Year, dt. Month, dt. Day );
- StringFileName = Path. Combine (dir, delimiter fileName );
- StreamWriter sw;
- If(File. Exists (fileName ))
- Sw = File. AppendText (fileName );
- Else
- Sw = File. CreateText (fileName );
- Sw. Write (txt. ToString ());
- Sw. Close ();
- Sw =Null;
- }
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.
- Protected VoidPage_Load (ObjectSender, EventArgs e)
- {
- StringBuilder txt =NewStringBuilder ();
- For(IntI = 0; I <100; I ++)
- {
- If(This. Response. IsClientConnected)
- {
- Txt. AppendLine ();
- Txt. AppendLine (DateTime. Now. ToString ("U"));
- Txt. AppendLine (I. ToString ());
- Response. Write (DateTime. Now. ToString ("U"));
- Response. Write (" \ R \ n");
- Thread. Sleep (500 );
- }
- Else
- {
- Response. End ();
- Return;
- }
- }
- Txt. AppendLine (DateTime. Now. ToString ("U"));
- Response. Write (DateTime. Now. ToString ("U"));
- Response. Write (" \ R \ n");
- // Write some information to another file to check whether the file is running
- StringDir = Path. Combine (AppDomain. CurrentDomain. BaseDirectory,"Logs");
- If(! Directory. Exists (dir ))
- Directory. CreateDirectory (dir );
- DateTime dt = DateTime. Now;
- StringRequired filename =String. Format ("Errors _ {}. log", Dt. Year, dt. Month, dt. Day );
- StringFileName = Path. Combine (dir, delimiter fileName );
- StreamWriter sw;
- If(File. Exists (fileName ))
- Sw = File. AppendText (fileName );
- Else
- Sw = File. CreateText (fileName );
- Sw. Write (txt. ToString ());
- Sw. Close ();
- Sw =Null;
- }
In this example, when an interruption is detected, nothing previously done is discarded.