user1785715 user1785715 - 3 months ago 34
C# Question

Millisecond interval for DispatcherTimer

I'm using a DispatcherTimer to call a void in C#:

counter = new System.Windows.Threading.DispatcherTimer();
counter.Tick += new EventHandler(counter_Tick);
counter.Interval = new TimeSpan(0, 0, 1);
counter.Start();


However I would like the interval to be a few milliseconds, TimeSpan's parameters are hours,minutes,seconds and only accepts integers (as far as I can see). Is there a work around for DispatcherTimer? I've tried Timer but I cannot use it (missing reference apparently)

Thanks in advance.

Answer

The TimeSpan object has a constructor that takes milliseconds as a parameter:

MSDN has the details: https://msdn.microsoft.com/en-us/library/6c7z43tw(v=vs.110).aspx

Basically, replace new TimeSpan(0, 0, 1) with new TimeSpan(0, 0, 0, 0, millisecondTimeout).