Only raise an event if the previous one was completed

后端 未结 4 777
误落风尘
误落风尘 2021-01-16 11:25

I\'m using a System.Timers.Timer in my application. Every second I run a function which does some job. The thing is, this function can block for some little tim

4条回答
  •  抹茶落季
    2021-01-16 12:01

    If you want to skip the tick if another is already working you can do this.

    private readonly object padlock = new object();
    
    private void SomeMethod()
    {
        if(!Monitor.TryEnter(padlock))
            return;
    
        try
        {
            //Do heavy work
        }
        finally
        {
            Monitor.Exit(padlock);
        }
    }
    

提交回复
热议问题