2011-12-08 22 views
0

讓我們假設我有這樣一個動作:在客戶端請求完全服務之後做一些清理工作,在c#ASP.net MVC中,怎麼樣?

public ActionResult Register() 
    { 
     // Doing user register 
     user_register(); // take almost no time 
     // doing some stats and other clean up 
     more_clean_up(); // take few seconds 
     return View(); 
    } 

這裏是我想達到的目標:

 user_register(); 
     return View(); 
     more_clean_up(); 

我知道上面的代碼將無法正常工作,more_clean_up();將永遠不會執行。 由於more_clean_up();任務對於view()並不重要,也需要時間,所以我希望它以某種方式執行而不會阻止當前用戶查看體驗。

我知道一種方法來打開另一個線程或異步去,但我更喜歡這裏,如果是一個辦法:

doing_some_render_for_client(); 
Response.Flush(); 
**disconnect_from_client();** 
more_clean_up(); 

有誰知道如何disconnect_from_client();但不像ApplicationInstance.CompleteRequest();

謝謝。

回答

1

你應該看看爲此使用ThreadPool產生一個新的線程。簡單地給它你的more_clean_up方法(你必須確保它有單個object參數),你應該全部設置。

更具體地回答你的第二個問題 - 不,你真的不應該試圖做到這一點。

相關問題