ASP.net 頁面被關(guān)閉后,服務(wù)器端是否仍然執(zhí)行中?
更新時間:2008年08月12日 22:21:02 作者:
當(dāng)一個正在執(zhí)行中的ASPX頁面執(zhí)行到一半的時候,瀏覽器中你關(guān)閉了這個頁面,服務(wù)器端對應(yīng)的這個頁面的代碼仍然在執(zhí)行么?
問題:當(dāng)一個正在執(zhí)行中的ASPX頁面執(zhí)行到一半的時候,瀏覽器中你關(guān)閉了這個頁面,服務(wù)器端對應(yīng)的這個頁面的代碼仍然在執(zhí)行么?
答案:除非你代碼里面做了特殊判斷,否則仍然正在執(zhí)行。
注意點:
1、客戶端顯示頁面的時候,后臺已經(jīng)執(zhí)行完了的頁面對象早已經(jīng)不存在了。當(dāng)然這時候談不上服務(wù)器段執(zhí)行不執(zhí)行的問題了。
2、頁面還沒有返回,處于等待狀態(tài)的時候。關(guān)閉ASPX頁面,才會涉及到上面提到的服務(wù)器端仍然在執(zhí)行的情況。
3、客戶端關(guān)閉的時候根本不向服務(wù)器發(fā)送指令。
4、除非你代碼里面做了特殊判斷,這里的特殊判斷指用 if(!Response.IsClientConnected) 來檢測狀態(tài)而用代碼終止運行。
下面的簡單代碼就是演示關(guān)閉頁面后,看是否仍然在執(zhí)行?
你可以在這個頁面打開后, 還沒有返回任何信息的時候把這個頁面關(guān)閉,然后看指定目錄下是否有對應(yīng)文件被創(chuàng)建并填寫內(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("<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 開發(fā)工具自帶的開發(fā)站點 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;
}這個例子中是發(fā)現(xiàn)中斷,就拋棄之前做的任何東西。
當(dāng)然我們也可以簡單的修改上述代碼,讓把已經(jīng)處理完成的東西記錄下來,類似下面的代碼
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 是要占用一定的系統(tǒng)資源的。
isClientConnected 實際上需要向客戶端輸出一點東西,然后才知道客戶端是否仍然在線。
這樣,除非你的應(yīng)用非常耗時,否則建議你不要用 isClientConnected 。 免得判斷 isClientConnected 使用的資源比你實際業(yè)務(wù)邏輯使用的資源還要多。
在任何情況下, Response.IsClientConnected 都要有些開銷,所以,只有在執(zhí)行至少要用 500 毫秒(如果想維持每秒幾十頁的吞吐量,這是一個很長的時間了)的操作前才使用它。作為通常的規(guī)則,不要在緊密循環(huán)的每次迭代中調(diào)用它,例如當(dāng)繪制表中的行,可能每 20 行或每 50 行調(diào)用一次。
答案:除非你代碼里面做了特殊判斷,否則仍然正在執(zhí)行。
注意點:
1、客戶端顯示頁面的時候,后臺已經(jīng)執(zhí)行完了的頁面對象早已經(jīng)不存在了。當(dāng)然這時候談不上服務(wù)器段執(zhí)行不執(zhí)行的問題了。
2、頁面還沒有返回,處于等待狀態(tài)的時候。關(guān)閉ASPX頁面,才會涉及到上面提到的服務(wù)器端仍然在執(zhí)行的情況。
3、客戶端關(guān)閉的時候根本不向服務(wù)器發(fā)送指令。
4、除非你代碼里面做了特殊判斷,這里的特殊判斷指用 if(!Response.IsClientConnected) 來檢測狀態(tài)而用代碼終止運行。
下面的簡單代碼就是演示關(guān)閉頁面后,看是否仍然在執(zhí)行?
你可以在這個頁面打開后, 還沒有返回任何信息的時候把這個頁面關(guān)閉,然后看指定目錄下是否有對應(yīng)文件被創(chuàng)建并填寫內(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("<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 開發(fā)工具自帶的開發(fā)站點 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;
}這個例子中是發(fā)現(xiàn)中斷,就拋棄之前做的任何東西。
當(dāng)然我們也可以簡單的修改上述代碼,讓把已經(jīng)處理完成的東西記錄下來,類似下面的代碼
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 是要占用一定的系統(tǒng)資源的。
isClientConnected 實際上需要向客戶端輸出一點東西,然后才知道客戶端是否仍然在線。
這樣,除非你的應(yīng)用非常耗時,否則建議你不要用 isClientConnected 。 免得判斷 isClientConnected 使用的資源比你實際業(yè)務(wù)邏輯使用的資源還要多。
在任何情況下, Response.IsClientConnected 都要有些開銷,所以,只有在執(zhí)行至少要用 500 毫秒(如果想維持每秒幾十頁的吞吐量,這是一個很長的時間了)的操作前才使用它。作為通常的規(guī)則,不要在緊密循環(huán)的每次迭代中調(diào)用它,例如當(dāng)繪制表中的行,可能每 20 行或每 50 行調(diào)用一次。
相關(guān)文章
解決Asp.net Mvc返回JsonResult中DateTime類型數(shù)據(jù)格式問題的方法
這篇文章主要介紹了解決Asp.net Mvc返回JsonResult中DateTime類型數(shù)據(jù)格式問題的方法,需要的朋友可以參考下2016-06-06C#實現(xiàn)HTTP協(xié)議迷你服務(wù)器(兩種方法)
用C#語言實現(xiàn)HTTP協(xié)議的服務(wù)器類本文將以兩種稍微有差別的方式用C#語言實現(xiàn);要完成高性能的Web服務(wù)功能,通常都是需要寫入到服務(wù),如IIS,Apache Tomcat感興趣的朋友可以了解下,或許對你學(xué)習(xí)c#有所幫助2013-02-02.NET?CPU爆高事故事故分析某供應(yīng)鏈WEB網(wǎng)站
這篇文章主要為大家介紹了.NET?CPU爆高事故事故分析某供應(yīng)鏈WEB網(wǎng)站,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06ASP.NET中MVC使用AJAX調(diào)用JsonResult方法并返回自定義錯誤信息
這篇文章主要介紹了ASP.NET中MVC使用AJAX調(diào)用JsonResult方法并返回自定義錯誤信息的相關(guān)資料,需要的朋友可以參考下2014-11-11ASP.NET+XML打造網(wǎng)絡(luò)硬盤原理分析
文件傳送常用的三種方式:FTP、Email及網(wǎng)上鄰居,都在一定程度上實現(xiàn)了文件數(shù)據(jù)的交流,但它們都主要面向“點對點”的傳送,無法實現(xiàn)一塊空間,資源互見的應(yīng)用需求,這種基于點對多的共享模式需要尋求另外的傳輸途徑,網(wǎng)絡(luò)硬盤就是一種很好的解決方式2012-09-09.Net?Api?中使用Elasticsearch存儲文檔的方法
Elasticsearch 是一個分布式、高擴展、高實時的搜索與數(shù)據(jù)分析引擎,在C# 的環(huán)境中,有一個Es的官方拓展包Nest,可以讓我們方便快捷的使用上Es數(shù)據(jù)庫,本文重點給大家介紹.Net?Api?中使用Elasticsearch存儲文檔的方法,感興趣的朋友一起看看吧2022-01-01DataGridView使用自定義控件實現(xiàn)簡單分頁功能(推薦)
這篇文章主要介紹了DataGridView使用自定義控件實現(xiàn)簡單分頁功能,數(shù)據(jù)庫使用的是sqlserver,本文通過通過實例代碼給大家講解的非常詳細,需要的朋友參考下吧2019-11-11