中途關閉ASP.NET是否影響服務器端執行?
在執行ASP.NET頁面過程中,總難免會出現這樣那樣的問題,比較典型的一個問題就是當一個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;
- }
這個例子中是發現中斷,就拋棄之前做的任何東西。 #p#
當然我們也可以簡單的修改上述代碼,讓把已經處理完成的東西記錄下來,類似下面的代碼
- 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 行調用一次。
【編輯推薦】