Is there a way to use one timer for multiple events?
You can set a timer to immediately disable the event or continue its execution (Timer.AutoReset property). Yes, you can register several different event handlers on the same timer, but I don't know there is any way to know in what order they will fire. If that matters to you, install one handler and call that handler with others. If you are trying to call a different handler every time the timer goes off, I suggest installing one handler that stores an enumeration indicating which function to call and incrementing it each time the timer is called.
To call the same handler to "iterate" through the list of parameters, once in each interval passed, I will have an array or list of parameters, and the handler will simply increment the counter or destroy the list.
using System.Timers;
public class MyTimedDelete {
private static List<int> ListOfIds=null;
private static System.Timers.Timer myTimer=null;
public static void AddIdToQueue(int id)
{
if (ListOfIds == null)
{
ListOfIds = new List<int>();
myTimer = new System.Timers.Timer(2000);
myTimer.Elapsed += OnTimedEvent;
}
ListOfIds.Add(id);
if (ListOfIds.Count==1)
{
myTimer.Start();
}
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
deleteItem(ListOfIds[0]);
ListOfIds.RemoveAt(0);
if (ListOfIds.Count == 0) {
myTimer.Stop();
}
}
}
source to share