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

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

问题:当一个正在执行中的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("
/r/n");            Thread.Sleep(50000);            txt.AppendLine(DateTime.Now.ToString("u"));            Response.Write(DateTime.Now.ToString("u"));            Response.Write("
/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("
/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");            // 把一些信息写到另外一个文件,借此察看是否正在运行            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("
/r/n");                    Thread.Sleep(500);                }                else                {                    break;                }            }            txt.AppendLine(DateTime.Now.ToString("u"));            Response.Write(DateTime.Now.ToString("u"));            Response.Write("
/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 行调用一次。

 

 

参考资料:

how to increase timeout on aspx page?  
http://p2p.wrox.com/topic.asp?TOPIC_ID=7504

asp.net能不能在客户端关闭后在后台继续将页面执行完毕? 
http://topic.csdn.net/u/20070202/14/85ea5576-907a-4960-9c53-b206a05228e4.html

在 ASP.NET 中使用计时器(Timer)  
http://blog.joycode.com/percyboy/articles/3595.aspx

ASP.NET 2.0 中的异步页 
http://www.microsoft.com/china/msdn/library/webservices/asp.net/issuesWickedCodetoc.mspx?mfr=true

IsClientConnected的问题 
http://topic.csdn.net/u/20080712/19/74fa4070-84bd-4fda-a99b-ac361f874738.html

Response.IsClientConnected 原理和用法 
http://blog.51ait.cn/article.asp?id=357    第一个地址比较慢,可以看下一个地址 
http://blog.joycode.com/ghj/archive/2008/07/23/115198.aspx

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值