Q:

Fire a timer foreach element one at a time?

For each element in the array I want to set a timer say of 2 seconds and then fire the event to return to the next element(with some additional logic I will stop the timer before starting new one), but the problem is that I can't lock the foreach until the timer is elapsed, I tried using lock statements but that didn't work for me. I had an idea of using Thread.Sleep(), but it seems it's not so efficient for the use case I need.
This code starts the timer and returns immediately all the elements in the array(which is not what I want to achieve).
public class Example
{
    private static System.Timers.Timer aTimer;

   public static void Main()
   {
      int[] randomNumbers= new int[]{1,2,4,5,645,65,56,5,645,6,546,45};

      foreach(var number in randomNumbers){
        SetTimer(2000);
        System.Console.WriteLine(number);
      }
   }

   private static void SetTimer(int timeInMiliseconds)
   {
        aTimer = new System.Timers.Timer(timeInMiliseconds);
        aTimer.Elapsed += OnTimedEvent;
        aTimer.AutoReset = false;
        aTimer.Enabled = true;
    }

    public static void StopTimer()
    {
        aTimer.Stop();
        aTimer.Dispose();
        System.Console.WriteLine("Timer stoped");
    }

    private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
         //Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}",
         //               e.SignalTime);
         // Go to the next element
    }
}

A:

You have to set the timer only once and not in every iteration of your for statement.
You can keep an index of your collection and at every Elapsed event of the timer you increment the index and retrieve the corresponding object.
When you retrieved all the elements, stop the timer.
Example
public class Example
{
    private static System.Timers.Timer aTimer;
    static int[] randomNumbers = new int[] { 1, 2, 4, 5, 645, 65, 56, 5, 645, 6, 546, 45 };
    static int index = 0;
    static readonly int arrayLength = randomNumbers.Length;

    public static void Main()
    {
        SetTimer(2000);
        Console.ReadKey();
    }

    private static void SetTimer(int timeInMiliseconds)
    {
        aTimer = new System.Timers.Timer(timeInMiliseconds);
        aTimer.Elapsed += OnTimedEvent;
        aTimer.Start();
    }

    public static void StopTimer()
    {
        aTimer.Stop();
        aTimer.Dispose();
        System.Console.WriteLine("Timer stopped");
    }

    private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
        if (index < arrayLength)
        {
            Console.WriteLine(randomNumbers[index]);
            index++;
        }
        else
            StopTimer();
    }
}

Tested and working

