欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Timer倒计时

程序员文章站 2022-06-08 22:32:06
...
  Timer Timer = new Timer(1000);
    public long lastTickTime = long.MinValue;
    public long HeartTime = 10;        


    void Start () {

        lastTickTime = GetTimeStamp();

        TimerInit();
  
    }

    //倒计时
    public void TimerInit()
    {
        Timer.Elapsed += new ElapsedEventHandler(HandleMainTimer);
        Timer.AutoReset = false;
        Timer.Enabled = true;
    }

    private void HandleMainTimer(object sender, ElapsedEventArgs e)
    {
        HearBeat();
        Timer.Start();
    }


    private void OnDestroy()
    {
        Timer.Stop();
    }


    
    public void HearBeat()
    {
    

        long timeNow = GetTimeStamp();
        long now = timeNow - lastTickTime;

         Debug.Log(now);

        if ((timeNow - lastTickTime) >= HeartTime)
        {
            Debug.Log("断线");
        }


    }

  
    

    //获取时间点
    public static long GetTimeStamp()
    {
        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
        return Convert.ToInt64(ts.TotalSeconds);
    }