-1
我正在開發ASP.NET5應用程序,並且我想在某些延遲後觸發服務器上的事件。我還希望客戶端能夠向服務器發送請求以取消事件的執行。 如何堅持Timer
,所以我可以通過撥打Change(Timeout.Infinite, Timeout.Infinite)
在另一個請求中取消它?在某些延遲後在服務器上引發事件
public class ApiController : Controller
{
public IActionResult SetTimer()
{
TimerCallback callback = new TimerCallback(EventToRaise);
Timer t = new Timer(callback, null, 10000, Timeout.Infinite);
//I need to persist Timer instance somehow in order to cancel the event later
return HttpOkObjectResult(timerId);
}
public IActionResult CancelTimer(int timerId)
{
/*
here I want to get the timer instance
and call Change(Timeout.Infinite, Timeout.Infinite)
in order to cancel the event
*/
return HttpOkResult();
}
private void EventToRaise(object obj)
{
///..
}
}
我使用System.Threading.Timer延遲EventToRaise
的執行,是我的方法正確,或者我應該做一些其他的方式?實現它的最好方法是什麼?
閱讀本=> http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx – CodeNotFound
@CodeNotFound謝謝先生,我想我會按照文章中的建議使用Hangfire。 – koryakinp