I want to call a function every X seconds. I read on SO about the the System.Threading.Timer and so I do:
Timer timer = new Timer(new TimerCallback(TimeCallBack), null, 0, 50000);
which calls the function:
void TimeCallBack();
However, I’d like to pass an argument to that function so that the timer calls the following function:
void TimeCallBack(MyClass mc);
I could make the instance I’m passing global but I’d rather pass it. From what I see the TimerCallback delegate is used in the Timer constructor. Any way I can use my own delegate/function call?
You can pass an argument to the callback through the
stateparameter of the Timer Constructor:Callback: