日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
中途關閉ASP.NET是否影響服務器端執(zhí)行?

在執(zhí)行ASP.NET頁面過程中,總難免會出現(xiàn)這樣那樣的問題,比較典型的一個問題就是當一個ASPX頁面執(zhí)行到一半的時候,瀏覽器關閉了這個頁面,那么服務器端對應的這個頁面的代碼還在執(zhí)行么?

網(wǎng)站建設哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、小程序制作、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了虎丘免費建站歡迎大家使用!

經(jīng)驗證明,除非你在代碼里面做了特殊判斷,否則代碼仍然正在執(zhí)行。

注意點:

1、客戶端顯示頁面的時候,后臺已經(jīng)執(zhí)行完了的頁面對象早已經(jīng)不存在了。當然這時候談不上服務器段執(zhí)行不執(zhí)行的問題了。

2、頁面還沒有返回,處于等待狀態(tài)的時候。關閉ASPX頁面,才會涉及到上面提到的服務器端仍然在執(zhí)行的情況。

3、客戶端關閉的時候根本不向服務器發(fā)送指令。

4、除非你代碼里面做了特殊判斷,這里的特殊判斷指用

 
 
 
  1. if(!Response.IsClientConnected) 

來檢測狀態(tài)而用代碼終止運行。

下面的簡單代碼就是演示關閉頁面后,看是否仍然在執(zhí)行?

你可以在這個頁面打開后, 還沒有返回任何信息的時候把這個頁面關閉,然后看指定目錄下是否有對應文件被創(chuàng)建并填寫內(nèi)容。

 
 
 
  1. protected void Page_Load(object sender, EventArgs e)   
  2. {   
  3. StringBuilder txt = new StringBuilder();   
  4. txt.AppendLine();   
  5. txt.AppendLine(DateTime.Now.ToString("u"));   
  6. txt.AppendLine("asvd");   
  7. Response.Write(DateTime.Now.ToString("u"));   
  8. Response.Write(" \r\n");   
  9. Thread.Sleep(50000);   
  10.  
  11. txt.AppendLine(DateTime.Now.ToString("u"));   
  12. Response.Write(DateTime.Now.ToString("u"));   
  13. Response.Write(" \r\n");   
  14. // 把一些信息寫到另外一個文件,借此察看是否正在運行   
  15. string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");   
  16. if (!Directory.Exists(dir))   
  17. Directory.CreateDirectory(dir);   
  18. DateTime dt = DateTime.Now;   
  19. string shortfileName = string.Format("errors_{0:0000}{1:00}{2:00}.log", dt.Year, dt.Month, dt.Day);   
  20. string fileName = Path.Combine(dir, shortfileName);   
  21. StreamWriter sw;   
  22. if (File.Exists(fileName))   
  23. sw = File.AppendText(fileName);   
  24. else   
  25. sw = File.CreateText(fileName);   
  26. sw.Write(txt.ToString());   
  27. sw.Close();   
  28. sw = null;   
  29. }  

作了特殊判斷的情況簡單例子:

注意: IsClientConnected 的判斷在 VS.net 開發(fā)工具自帶的開發(fā)站點 ASP.NET Development Server 是不支持的。 ASP.NET Development Server 永遠返回 true 。
IIS 才是支持的。

 
 
 
  1. protected void Page_Load(object sender, EventArgs e)   
  2. {   
  3. StringBuilder txt = new StringBuilder();   
  4. for (int i = 0; i < 100; i++)   
  5. {   
  6. if (this.Response.IsClientConnected)   
  7. {   
  8. txt.AppendLine();   
  9. txt.AppendLine(DateTime.Now.ToString("u"));   
  10. txt.AppendLine(i.ToString());   
  11. Response.Write(DateTime.Now.ToString("u"));   
  12. Response.Write(" \r\n");   
  13. Thread.Sleep(500);   
  14. }   
  15. else   
  16. {   
  17. Response.End();   
  18. return;   
  19. }   
  20. }   
  21. txt.AppendLine(DateTime.Now.ToString("u"));   
  22. Response.Write(DateTime.Now.ToString("u"));   
  23. Response.Write(" \r\n");   
  24. // 把一些信息寫到另外一個文件,借此察看是否正在運行   
  25. string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");   
  26. if (!Directory.Exists(dir))   
  27. Directory.CreateDirectory(dir);   
  28. DateTime dt = DateTime.Now;   
  29. string shortfileName = string.Format("errors_{0:0000}{1:00}{2:00}.log", dt.Year, dt.Month, dt.Day);   
  30. string fileName = Path.Combine(dir, shortfileName);   
  31. StreamWriter sw;   
  32. if (File.Exists(fileName))   
  33. sw = File.AppendText(fileName);   
  34. else   
  35. sw = File.CreateText(fileName);   
  36. sw.Write(txt.ToString());   
  37. sw.Close();   
  38. sw = null;   

這個例子中是發(fā)現(xiàn)中斷,就拋棄之前做的任何東西。 #p#

當然我們也可以簡單的修改上述代碼,讓把已經(jīng)處理完成的東西記錄下來,類似下面的代碼

 
 
 
  1. protected void Page_Load(object sender, EventArgs e)   
  2. {   
  3. StringBuilder txt = new StringBuilder();   
  4. for (int i = 0; i < 100; i++)   
  5. {   
  6. if (this.Response.IsClientConnected)   
  7. {   
  8. txt.AppendLine();   
  9. txt.AppendLine(DateTime.Now.ToString("u"));   
  10. txt.Append("********** ");   
  11. txt.AppendLine(i.ToString());   
  12. Response.Write(DateTime.Now.ToString("u"));   
  13. Response.Write(" \r\n");   
  14. Thread.Sleep(500);   
  15. }   
  16. else   
  17. {   
  18. break;   
  19. }   
  20. }   
  21. txt.AppendLine(DateTime.Now.ToString("u"));   
  22. Response.Write(DateTime.Now.ToString("u"));   
  23. Response.Write(" \r\n");   
  24. // 把一些信息寫到另外一個文件,借此察看是否正在運行   
  25. string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");   
  26. if (!Directory.Exists(dir))   
  27. Directory.CreateDirectory(dir);   
  28. DateTime dt = DateTime.Now;   
  29. string shortfileName = string.Format("errors_{0:0000}{1:00}{2:00}.log", dt.Year, dt.Month, dt.Day);   
  30. string fileName = Path.Combine(dir, shortfileName);   
  31. StreamWriter sw;   
  32. if (File.Exists(fileName))   
  33. sw = File.AppendText(fileName);   
  34. else   
  35. sw = File.CreateText(fileName);   
  36. sw.Write(txt.ToString());   
  37. sw.Close();   
  38. sw = null;   

需要注意的是, 使用 isClientConnected 是要占用一定的系統(tǒng)資源的。

isClientConnected 實際上需要向客戶端輸出一點東西,然后才知道客戶端是否仍然在線。

這樣,除非你的應用非常耗時,否則建議你不要用 isClientConnected 。 免得判斷 isClientConnected 使用的資源比你實際業(yè)務邏輯使用的資源還要多。

在任何情況下, Response.IsClientConnected 都要有些開銷,所以,只有在執(zhí)行至少要用 500 毫秒(如果想維持每秒幾十頁的吞吐量,這是一個很長的時間了)的操作前才使用它。作為通常的規(guī)則,不要在緊密循環(huán)的每次迭代中調(diào)用它,例如當繪制表中的行,可能每 20 行或每 50 行調(diào)用一次。


分享標題:中途關閉ASP.NET是否影響服務器端執(zhí)行?
瀏覽地址:http://m.5511xx.com/article/dpeiejo.html