中途關(guān)閉ASP.NET是否影響服務(wù)器端執(zhí)行?
在執(zhí)行ASP.NET頁(yè)面過(guò)程中,總難免會(huì)出現(xiàn)這樣那樣的問(wèn)題,比較典型的一個(gè)問(wèn)題就是當(dāng)一個(gè)ASPX頁(yè)面執(zhí)行到一半的時(shí)候,瀏覽器關(guān)閉了這個(gè)頁(yè)面,那么服務(wù)器端對(duì)應(yīng)的這個(gè)頁(yè)面的代碼還在執(zhí)行么?
經(jīng)驗(yàn)證明,除非你在代碼里面做了特殊判斷,否則代碼仍然正在執(zhí)行。
注意點(diǎn):
1、客戶端顯示頁(yè)面的時(shí)候,后臺(tái)已經(jīng)執(zhí)行完了的頁(yè)面對(duì)象早已經(jīng)不存在了。當(dāng)然這時(shí)候談不上服務(wù)器段執(zhí)行不執(zhí)行的問(wèn)題了。
2、頁(yè)面還沒(méi)有返回,處于等待狀態(tài)的時(shí)候。關(guān)閉ASPX頁(yè)面,才會(huì)涉及到上面提到的服務(wù)器端仍然在執(zhí)行的情況。
3、客戶端關(guān)閉的時(shí)候根本不向服務(wù)器發(fā)送指令。
4、除非你代碼里面做了特殊判斷,這里的特殊判斷指用
- if(!Response.IsClientConnected)
來(lái)檢測(cè)狀態(tài)而用代碼終止運(yùn)行。
下面的簡(jiǎn)單代碼就是演示關(guān)閉頁(yè)面后,看是否仍然在執(zhí)行?
你可以在這個(gè)頁(yè)面打開(kāi)后, 還沒(méi)有返回任何信息的時(shí)候把這個(gè)頁(yè)面關(guān)閉,然后看指定目錄下是否有對(duì)應(yīng)文件被創(chuàng)建并填寫(xiě)內(nèi)容。
- 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");- // 把一些信息寫(xiě)到另外一個(gè)文件,借此察看是否正在運(yù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;
- }
作了特殊判斷的情況簡(jiǎn)單例子:
注意: IsClientConnected 的判斷在 VS.net 開(kāi)發(fā)工具自帶的開(kāi)發(fā)站點(diǎn) ASP.NET Development Server 是不支持的。 ASP.NET Development Server 永遠(yuǎn)返回 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");- // 把一些信息寫(xiě)到另外一個(gè)文件,借此察看是否正在運(yù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;
- }
這個(gè)例子中是發(fā)現(xiàn)中斷,就拋棄之前做的任何東西。 #p#
當(dāng)然我們也可以簡(jiǎn)單的修改上述代碼,讓把已經(jīng)處理完成的東西記錄下來(lái),類似下面的代碼
- 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");- // 把一些信息寫(xiě)到另外一個(gè)文件,借此察看是否正在運(yù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 是要占用一定的系統(tǒng)資源的。
isClientConnected 實(shí)際上需要向客戶端輸出一點(diǎn)東西,然后才知道客戶端是否仍然在線。
這樣,除非你的應(yīng)用非常耗時(shí),否則建議你不要用 isClientConnected 。 免得判斷 isClientConnected 使用的資源比你實(shí)際業(yè)務(wù)邏輯使用的資源還要多。
在任何情況下, Response.IsClientConnected 都要有些開(kāi)銷,所以,只有在執(zhí)行至少要用 500 毫秒(如果想維持每秒幾十頁(yè)的吞吐量,這是一個(gè)很長(zhǎng)的時(shí)間了)的操作前才使用它。作為通常的規(guī)則,不要在緊密循環(huán)的每次迭代中調(diào)用它,例如當(dāng)繪制表中的行,可能每 20 行或每 50 行調(diào)用一次。
【編輯推薦】