After the Asp.net page is disabled, is the server still running?

Source: Internet
Author: User

Problem: When An ASPX page in progress is halfway through, you close this page in the browser and the server corresponds to this page Code Still executing?

Answer: Unless you have made a special judgment in your code, it is still being executed.

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 you have made a special judgment in your code, the special judgment here refers to using if (! Response. isclientconnected) to detect the status and terminate the running with the code.

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 void page_load (Object sender, eventargs E)
{
Stringbuilder TXT = new stringbuilder ();

TXT. appendline ();
TXT. appendline (datetime. Now. tostring ("U "));
TXT. appendline ("asvd ");

Response. Write (datetime. Now. tostring ("U "));
Response. Write ("<br/> \ r \ n ");
Thread. Sleep (50000 );

TXT. appendline (datetime. Now. tostring ("U "));
Response. Write (datetime. Now. tostring ("U "));
Response. Write ("<br/> \ r \ n ");

// Write some information to another file to check whether the file is running
String dir = path. Combine (appdomain. currentdomain. basedirectory, "logs ");
If (! Directory. exists (DIR ))
Directory. createdirectory (DIR );
Datetime dt = datetime. now;
String parameter filename = string. Format ("errors _ {0: 0000} {}. log", DT. Year, DT. Month, DT. Day );
String filename = 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 void page_load (Object sender, eventargs E)
{

Stringbuilder TXT = new stringbuilder ();

For (INT I = 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 ("<br/> \ r \ n ");
Thread. Sleep (500 );
}
Else
{
Response. End ();
Return;
}
}

TXT. appendline (datetime. Now. tostring ("U "));
Response. Write (datetime. Now. tostring ("U "));
Response. Write ("<br/> \ r \ n ");

// Write some information to another file to check whether the file is running
String dir = path. Combine (appdomain. currentdomain. basedirectory, "logs ");
If (! Directory. exists (DIR ))
Directory. createdirectory (DIR );
Datetime dt = datetime. now;
String parameter filename = string. Format ("errors _ {0: 0000} {}. log", DT. Year, DT. Month, DT. Day );
String filename = 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, we find the interrupt and discard anything we did before.

Of course, we can also simply modify the above Code to record the processed items, similar to the following code.

Protected void page_load (Object sender, eventargs E)
{
Stringbuilder TXT = new stringbuilder ();

For (INT I = 0; I <100; I ++)
{
If (this. response. isclientconnected)
{
TXT. appendline ();
TXT. appendline (datetime. Now. tostring ("U "));
TXT. append ("**********");
TXT. appendline (I. tostring ());

Response. Write (datetime. Now. tostring ("U "));
Response. Write ("<br/> \ r \ n ");
Thread. Sleep (500 );
}
Else
{
Break;
}
}

TXT. appendline (datetime. Now. tostring ("U "));
Response. Write (datetime. Now. tostring ("U "));
Response. Write ("<br/> \ r \ n ");

// Write some information to another file to check whether the file is running
String dir = path. Combine (appdomain. currentdomain. basedirectory, "logs ");
If (! Directory. exists (DIR ))
Directory. createdirectory (DIR );
Datetime dt = datetime. now;
String parameter filename = string. Format ("errors _ {0: 0000} {}. log", DT. Year, DT. Month, DT. Day );
String filename = 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;
} It should be noted that using isclientconnected occupies certain system resources.

Isclientconnected needs to output something to the client before knowing whether the client is still online.

In this way, unless your application is very time-consuming, we recommend that you do not use isclientconnected. In case that isclientconnected uses more resources than your actual business logic.

Response. isclientconnected requires a certain amount of overhead, so it is used only when the execution takes at least 500 milliseconds (if you want to maintain the throughput of dozens of pages per second, this is a long time. As a general rule, do not call it in every iteration of a tight loop. For example, when the rows in the table are drawn, it may be called every 20 or 50 rows.

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.