Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get System.Threading.Timer period

Tags:

c#

timer

I use a System.Threading.Timer object, and I set its period:

// period of 1 second
var timer = new Timer(CallBack, null, 0, 1000);

...

Now I want to get the period of my timer, how can I do?

like image 399
Nick Avatar asked Oct 12 '25 21:10

Nick


1 Answers

There is no supported way to do that. You have two options:

1) It's best to use System.Timers.Timer class, as it's more flexible, and safer to use in case of multi threaded use. If you want to know more about timers, and comparison between System.Windows.Forms.Timer, System.Timers.Timer and System.Threading.Timer, please read MSDN article about this subject.

2) Use reflection to access private member holding the period value. There are plenty of nice articles on how to do this, and you can use ILSpy to see which field you need to read. (it's timer.m_timer.m_timer.m_period in .NET 4.0, it may be the same in other versions too)

like image 79
Nikola Radosavljević Avatar answered Oct 14 '25 11:10

Nikola Radosavljević



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!