简体   繁体   English

拍卖计时器在 Asp.net webAPI 中未按预期工作

[英]Auction Timer is not working as expected in Asp.net webAPI

public class AuctionTimer : IDisposable
{
    public static readonly ConcurrentDictionary<string, AuctionTimer> Timers;

    private readonly Timer timer;

    static AuctionTimer()
    {
        Timers = new ConcurrentDictionary<string, AuctionTimer>();
    }

    private AuctionTimer(string AuctionID, int ExecutionTime)
    {
        this._AuctionID = AuctionID;
        timer = new Timer();
        TimeCount = ExecutionTime;
        timer.Interval = new TimerUtil().getAuctionInterval();
        timer.Elapsed += (s, e) => MonitorElapsedTime();
        timer.Start();
    }

    private int TimeCount { get; set; }

    private string _AuctionID { get; set; }

    public static void StartTimer(string AuctionID, int ExecutionTime)
    {
        var newTimer = new AuctionTimer(AuctionID, ExecutionTime);
        if (!Timers.TryAdd(AuctionID, newTimer))
        {
            newTimer.Dispose();
        }
    }

    public static void StopTimer(string AuctionID)
    {
        AuctionTimer oldTimer;
        if (Timers.TryRemove(AuctionID, out oldTimer))
        {
            oldTimer.Dispose();
        }
    }


    public void Dispose()
    {
        // Stop might not be necessary since we call Dispose
        timer.Stop();
        timer.Dispose();
    }

    private void MonitorElapsedTime()
    {
        if (TimeCount == new TimerUtil().getAuctionInterval())
        {
            StopTimer(_AuctionID);
            //do the other process in db 
        }
        else
        {
            TimeCount++;
        }
    }
}

Following is the code for my Auction Timer what i am trying to do here is making an auction storing starting and ending date so lets say an auction is going to last for 5 hours so the end date will be 5 hours from current time i am starting the Auction timer right after storing the end date for the auction it is working as expected but the problem i am facing now is the timer is bit delay from the end time around 2,3 minutes roughly that's means the process which is suppose to run immediately after the auction expires is running 2,3 minutes late i am not really sure what i am missing here.以下是我的拍卖计时器的代码我在这里尝试做的是制作一个存储开始和结束日期的拍卖,所以假设拍卖将持续 5 小时,所以结束日期将从我开始的当前时间开始 5 小时拍卖计时器在存储拍卖的结束日期后立即按预期工作,但我现在面临的问题是计时器从结束时间延迟了大约 2.3 分钟,这意味着该过程应该立即运行拍卖到期后,我迟到了 2.3 分钟,我不太确定我在这里错过了什么。

我能够使用这个框架解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM