-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