我正在开发一个停车场应用程序,我想要30秒后可用的停车场设置超时。我正在使用system.timer实现它。这是我的代码:
internal class UpdateAvailableCarparkCommand
{
private readonly CarParkContext _context;
private readonly ApplicationUser _user;
private readonly DateTime _availableDate;
//constructor
//...
private void SetTimeOut{
//set the timeOut field in the database
var existingCarPark = _context.AvailableCarParks.FirstOrDefault(cp => cp.CarParkOwner.Id == _user.Id && cp.DayAvailable == _availableDate.Date);
existingCarPark.timeOut = true;
_context.SaveChanges();
}
private AvailableCarPark{
//create a new availableCarpark
var availableCarPark = new AvailableCarPark{
//...
//...
}
//After an availableCarpark is created start the timer
//timer wait for 30 seconds then timeout the availableCarpark
_aTimer = new System.Timers.Timer(30000);
// Hook up the Elapsed event for the timer.
_aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
_aTimer.AutoReset = false;
_aTimer.Enabled = true;
return availableCarPark;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
SetTimeOut();
}
但是从不使用计时器调用SetTimeOut(),我做错了什么? 我也尝试过使用System.Threading计时器,但没有使其正常工作。如果threading.timer更好,该如何实现?感谢您的任何建议。