Is the server side still executing after the ASP.net page is closed?

Source: Internet
Author: User
Tags datetime sleep

Question: When an ongoing ASPX page executes halfway through the browser, you close the page and the server-side code for this page is still executing?

Answer: Unless you make a special judgment in your code, you are still executing.

Note the point:

1, the client Display page, the background has been executed by the Page object has not existed. Of course this is not a problem with server segment execution.

2, the page has not returned, in the waiting state. Closing the ASPX page will involve situations where the server side mentioned above is still executing.

3. When the client shuts down, it does not send instructions to the server at all.

4, unless you make a special judgment in the code, here the special judgment refers to the IF (! response.isclientconnected) to detect the status and terminate the operation with code.

The following simple code is a demo of closing the page to see if it's still executing?

You can turn this page off when you have not returned any information after the page is opened, and then see if any corresponding files are created and filled out in the specified directory.

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 ("\ 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 see if it is running
string dir = Path.Combine (AppDomain.CurrentDomain.BaseDirectory, "logs");
if (!        Directory.Exists (dir))
Directory.CreateDirectory (dir);
DateTime dt = DateTime.Now;
String shortfilename = string. Format ("Errors_{0:0000}{1:00}{2:00}.log", dt. Year, dt. Month, dt. Day);
String fileName = Path.Combine (dir, ShortFileName);
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:

Note: IsClientConnected's judgment is not supported by the development site ASP.net Development Server that vs.net development tools bring. 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 < 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 see if it is running
string dir = Path.Combine (AppDomain.CurrentDomain.BaseDirectory, "logs") ;
if (!        Directory.Exists (dir))
Directory.CreateDirectory (dir);
DatetIME dt = DateTime.Now;
String shortfilename = string. Format ("Errors_{0:0000}{1:00}{2:00}.log", dt. Year, dt. Month, dt. Day);
String fileName = Path.Combine (dir, ShortFileName);
StreamWriter SW;       
if (file.exists (filename))
SW = file.appendtext (filename);
      else
SW = File.createtext (fileName);
Sw. Write (txt. ToString ());
SW. Close ();
SW = null;
}

Related Article

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.