Friends, Technology, Web2.0 - What I am reading

    [Home] [Recent] [Site Map]

   

Wednesday, July 23, 2008

7/23/2008 7:47:27 AM ASP.net 页面被关闭后,服务器端是否仍然执行中? >> joycode

问题:当一个正在执行中的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 行调用一次。

 

 

参考资料:

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

7/23/2008 12:50:38 PM 涂改网:轻量级在线图片编辑器 >> 分享网络2.0

涂改网是一款较为轻量级在线图片编辑器,提供了类似于Snipshot的基础服务。

涂改网 是一款新近浮出水面的Web应用程序,他们提供了类似于Snipshot的在线图片编辑基础服务,由于涂改网基于Ajax技术,用户的相应和处理速度很快。目前,涂改网已经发布了图片放缩、照片裁剪、照片旋转、图片调整亮度/对比度等基础编辑工具,同时还提供了多种照片的艺术效果处理以及开放的API接口等。

Snipshot 和 TinyURL 是两款极其不起眼的在线应用,但是他们却都获得了巨大的发展空间,你会惊讶的发现几乎绝大多数涉及到图片处理以及网址瘦身的平台模块中(OpenItOnline, Twitter等)都有他们的身影,而这一切都得益于他们开放的API;因此,我觉得,如果Web应用程序本身没有太多亮点,而被介入的门槛又底的可怜的话,开放API无疑是寻求可持续发展的一条终南捷径。


Copyright © 2008 本站作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可 ...

7/23/2008 9:36:10 PM Google Knol Is Live >> Google Blogoscoped

Google went live with Knol, a platform to read and write articles on all kinds of subjects. Knol was being tested privately since some time and had been pre-announced back in 2007. The address is knol.google.com, but notably not knol.com or knol.org or even googleknol.com. This project is somewhat reminiscent of Wikipedia, though there are many differences as well. You may also think of this as an alternative to creating a small info website if your aim is to cover only a single subject. When you log-in within your Google account to write an article on a subject you"re familiar with, yo ...

7/23/2008 7:46:46 PM Google App Engine Perl Project Started >> Google Blogoscoped

The Google App Engine currently supports Python, but now Google employee and creator of blogging community LiveJournal Brad Fitzpatrick posts this bit: I"m happy to announce that the Google App Engine team has given me permission to talk about a 20% project inside Google to to add Perl support to App Engine. To be clear: I"m not a member of the App Engine team and the App Engine team is not promising to add Perl support. They"re just saying that I (along with other Perl hackers here at Google) are now allowed to work on this 20% project of ours out in the open where other Perl hackers can help us out, should you be so inclined. Why is Google making this open? Turns out, according to Google, th ...

7/23/2008 2:13:44 PM Google on One Boxes and Grouping Result Types to Fill Informational Needs >> seobythesea

When stockbrokers who spend their day searching for financial information about different businesses type the word “Starbucks” into Google’s search box, chances are that they are more likely to be looking for stock price information than the closest place that they can get a mint mocha chip frappuccino. When a city-dwelling college student, who likes [...]

7/23/2008 8:19:20 PM Women Love the Michelin Man >> Bruce Clay, Inc. Blog

As you know, I was at BlogHer last week. And another conference means another tote bag filled with goodies that you have to lug around. The BlogHer bag was like the typical SES schwag bag, only cooler and prettier and more useful. There were pens and shirts and books and...

7/23/2008 12:15:53 AM SEO Headlines >> Bruce Clay, Inc. Blog

Facebook Redesigns, Takes Focus Off Apps Everyone"s talking about the new Facebook redesign and it"s not all good. It seems like Facebook has strayed away from what they originally promised and has instead taken on a new FriendFeed feel where all updates are combined into a single content stream. ReadWriteWeb...

7/23/2008 12:45:33 PM Zh/首頁 - IEs4Linux IEs4Linux是一個讓您可以更簡單地在 Linux 上執行 微軟網路探險家(IE) (或是任何可以執行 Wine 的作業系統平台) [del.icio.us] >> 车东[Blog^2]

無需繁複的滑鼠點選,沒有無聊的安裝程序,也沒有複雜的 Wine 設定。只有一個簡單的腳本檔(script)您可以一次取得三個版本的IE來測試你的網站。同時這個 script 是自由軟體並且是開放原始碼

7/23/2008 10:08:49 AM 开发者的10个基本开发技术 Top 10 Concepts That Every Software Engineer Should Know - ReadWriteWeb [del.icio.us] >> 车东[Blog^2]

1. Interfaces 2. Conventions and Templates 3. Layering 4. Algorithmic Complexity 5. Hashing 6. Caching 7. Concurrency 8. Cloud Computing 9. Security 10. Relational Databases

7/23/2008 4:38:42 AM 姚明@LinkedIn Yao Ming - LinkedIn [del.icio.us] >> 车东[Blog^2]

姚明在LinkedIn上目前有8个联系人,离我3度。最新姚明的有奖问题咨询: 体育是否在你的工作生活中的起到重要作用? Has participating in sports made an important difference in your professional life?


^==Back Home

<==7/22/2008

==> 7/24/2008

Month Archives:

Top Tags:
Google Internet Technology Company & Product Profiles Search feature Business and Technology column Web2.0 analysis 服务介绍 comment application letter 业界信息 news China2.0 Startups 產業策進 deal 未來趨勢 Search Headlines 創投 业界动态 創業案例 Social Network Google/SEO widget news_in


@2007 All rights Reserved