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();
謝謝。