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

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

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

2019-11-17 03:58:29
字體:
來源:轉載
供稿:網友
問題:當一個正在執行中的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 行調用一次。



本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/dz45693/archive/2009/12/17/5027944.aspx
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平乡县| 竹北市| 林甸县| 渭南市| 云霄县| 财经| 西安市| 洪湖市| 沧州市| 长白| 呼伦贝尔市| 五河县| 九龙坡区| 板桥市| 信宜市| 蓝山县| 德庆县| 泰来县| 微博| 当阳市| 额济纳旗| 乌兰县| 永兴县| 仁怀市| 安福县| 筠连县| 广河县| 康平县| 普兰店市| 临湘市| 罗平县| 古蔺县| 库车县| 新沂市| 克拉玛依市| 延津县| 西乡县| 阿鲁科尔沁旗| 仁布县| 乌拉特中旗| 天门市|