How to block a timer while processing the elapsed event?

后端 未结 5 1772
臣服心动
臣服心动 2021-02-13 09:00

I have a timer that needs to not process its elapsed event handler at the same time. But processing one Elapsed event may interfere with others. I implemented the bel

5条回答
  •  耶瑟儿~
    2021-02-13 09:33

    I use the System.Threading.Timer like so

     class Class1
        {
            static Timer timer = new Timer(DoSomething,null,TimeSpan.FromMinutes(1),TimeSpan.FromMinutes(1));
    
            private static void DoSomething(object state)
            {
                timer = null; // stop timer
    
                // do some long stuff here
    
                timer = new Timer(DoSomething, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
            }
    
    
    
        }
    

提交回复
热议问题