I have a System.Threading.Timer which fires frequently (let's say every second for simplicity), in the CallBack I need to call an Action (which is passed in via the constructor, so sits in another class) within which I do some processing (let's say it takes 2+ seconds), how would I prevent my processing logic from being called multiple times? It seems that a lock() doesn't work within the Action call? I am using .net 3.5.
public TestOperation(Action callBackMethod)
{
this.timer = new System.Threading.Timer(timer_Elapsed, callbackMethod, timerInterval, Timeout.Infinite);
}
private void timer_Elapsed(object state)
{
Action callback = (Action) state;
if (callback != null)
{
callback();
}
}
// example of the callback, in another class.
private void callBackMethod()
{
// How can I stop this from running every 1 second? Lock() doesn't seem to work here
Thread.Sleep(2000);
}
Thanks!