DispatcherTimer equivalent

I need the equivalent of a .net DispatcherTimer object.

I tried this, but it does not tick. Besides using Update loop and the elapsed time, is there a more generic timer class, and how is it properly instantiated?

using UnityEngine;
using System.Collections;
using System.Timers;

public class ApplicationLaunch : MonoBehaviour {
	
	System.Timers.Timer timer1 = new System.Timers.Timer();
	
	// Use this for initialization
	void Start () 
	{
		timer1 = new Timer();
		timer1.Interval = 0.1;
		timer1.Elapsed +=TimerTick;

		timer1.Start();

	}
	
	void TimerTick(object o, System.EventArgs e)
	{
	       Debug.Log("Tick");
	}

Look into Invoke, InvokeRepeating, and coroutines.

–

1 Answer

1

The answer turned out to be fairly simple.
Interval is in milliseconds, which (apparently) will not update less than 1.
I changed it to say 100f, and away it ticks.

Invoke, InvokeRepeating have a lot of limitations for implementing as timers. You cannot specify method strings which take arguments for example, so if you have multiple timers for example, you have unique handlers. Coroutines have similar issues. This class is ideal for multiple non-framerate timers. Matrix covariance calculations for example. Any non-ui thread use.

Thanks for the tips.