0

我正在开发一个停车场应用程序,我希望在 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 更好,我该如何实现呢?感谢您的任何建议。

4

0 回答 0