国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.net 頁面被關閉后,服務器端是否仍然執行中?

2019-11-17 04:30:21
字體:
來源:轉載
供稿:網友

問題:當一個正在執行中的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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 四子王旗| 广昌县| 凌源市| 广灵县| 北宁市| 台山市| 鹤壁市| 临高县| 乌兰察布市| 黄骅市| 蓬安县| 陈巴尔虎旗| 长白| 武夷山市| 宁都县| 原平市| 大石桥市| 海盐县| 诸城市| 社旗县| 黄大仙区| 巴林左旗| 宿州市| 嘉义市| 莒南县| 中山市| 漾濞| 原平市| 柳河县| 贵定县| 时尚| 郧西县| 阜南县| 贡嘎县| 望奎县| 德惠市| 平乐县| 阿拉善左旗| 怀安县| 连云港市| 大同市|