当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.net 页面被关闭后,服务器端是否仍然执行中?

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

2018年04月21日  | 移动技术网IT编程  | 我要评论
问题:当一个正在执行中的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 行调用一次。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网