当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.net 页面被关闭后,服务器端是否仍然执行中?

ASP.NET
FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
.NET 常用功能和代码小结
在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
asp.net IList查询数据后格式化数据再绑定控件
asp.net sql存储过程
asp.net 简单实现禁用或启用页面中的某一类型的控件
asp.net(c#)获取内容第一张图片地址的函数
The remote procedure call failed and did not execute的解决办法
ASP.NET 在线文件管理
asp.net 读取并修改config文件实现代码
ASP.NET Cookie 操作实现
asp.net Silverlight中的模式窗体
Silverlight中动态获取Web Service地址
asp.net Silverlight应用程序中获取载体aspx页面参数
asp.net 水晶报表隔行换色实现方法
asp.net 获取Gridview隐藏列的值
手动把asp.net的类生成dll文件的方法
asp.net 使用ObjectDataSource控件在ASP.NET中实现Ajax真分页
动态指定任意类型的ObjectDataSource对象的查询参数
asp.net Md5的用法小结

ASP.NET 中的 ASP.net 页面被关闭后,服务器端是否仍然执行中?


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 100 ::
收藏到网摘: n/a

当一个正在执行中的ASPX页面执行到一半的时候,浏览器中你关闭了这个页面,服务器端对应的这个页面的代码仍然在执行么? 问题:当一个正在执行中的ASPX页面执行到一半的时候,浏览器中你关闭了这个页面,服务器端对应的这个页面的代码仍然在执行么?
答案:除非你代码里面做了特殊判断,否则仍然正在执行。
注意点:
1、客户端显示页面的时候,后台已经执行完了的页面对象早已经不存在了。当然这时候谈不上服务器段执行不执行的问题了。
2、页面还没有返回,处于等待状态的时候。关闭ASPX页面,才会涉及到上面提到的服务器端仍然在执行的情况。
3、客户端关闭的时候根本不向服务器发送指令。
4、除非你代码里面做了特殊判断,这里的特殊判断指用 if(!Response.IsClientConnected) 来检测状态而用代码终止运行。
下面的简单代码就是演示关闭页面后,看是否仍然在执行?
你可以在这个页面打开后, 还没有返回任何信息的时候把这个页面关闭,然后看指定目录下是否有对应文件被创建并填写内容。
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");
// 把一些信息写到另外一个文件,借此察看是否正在运行
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;
}
作了特殊判断的情况简单例子:
注意: IsClientConnected 的判断在 VS.net 开发工具自带的开发站点 ASP.NET Development Server 是不支持的。 ASP.NET Development Server 永远返回 true 。
IIS 才是支持的。
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");
// 把一些信息写到另外一个文件,借此察看是否正在运行
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;
}这个例子中是发现中断,就抛弃之前做的任何东西。
当然我们也可以简单的修改上述代码,让把已经处理完成的东西记录下来,类似下面的代码
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");
// 把一些信息写到另外一个文件,借此察看是否正在运行
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;
}需要注意的是, 使用 isClientConnected 是要占用一定的系统资源的。
isClientConnected 实际上需要向客户端输出一点东西,然后才知道客户端是否仍然在线。
这样,除非你的应用非常耗时,否则建议你不要用 isClientConnected 。 免得判断 isClientConnected 使用的资源比你实际业务逻辑使用的资源还要多。
在任何情况下, Response.IsClientConnected 都要有些开销,所以,只有在执行至少要用 500 毫秒(如果想维持每秒几十页的吞吐量,这是一个很长的时间了)的操作前才使用它。作为通常的规则,不要在紧密循环的每次迭代中调用它,例如当绘制表中的行,可能每 20 行或每 50 行调用一次。