Question: At the moment I have a For loop within a Callaback procedure :

```
private void InitializeComponent()
{

var timer = new System.Threading.Timer(TimerProc, null, 60000, 60000);

}

public void TimerProc(object state)
{

    for (int i = 0; i <= 6; i++)
    {
        //Do something here
    }

}

```

TimerProc only fires ONCE despite the fact that I have it set to repeat every 60000 milliseconds. It looks like that the operations within the loop are no longer executed after they are completed the first time around.

However, if I remove the For loop and have some other action like print to a console line or write to a text file then TimerProc repeats as expected.

Why is this the case and why does a loop with in a Callback procedure stop it from executing after its initial firing?

I have made a workaround by using a while(true) infinte loop and using the Thread.Timer to pause the process after x amount of milliseconds.

My new vised code with the while loop looks like this:

```
private void InitializeComponent()
{

processthread = new Thread(new ThreadStart(DoSomething));
processthread.Start();

}

public void DoSomething()

{

  while(true)
  {
    for (int i = 0; i <= 6; i++)
    {
           //do something here

    }
    Thread.Sleep(5000);
  }

}

```

I just want to know why callback procedure in the System.Thread.Timer cannot handle loops.

Cheers,

Stanley

Answer: 
From the code you've included in the question, it *looks* like you aren't storing a reference to `timer` anywhere. I could be wrong, but my guess is that you're not seeing the callback repeated because the timer object itself has been garbage collected. Assign a reference to the timer to a longer-lived variable (i.e., a field) and I'd wager that will resolve the problem.

In other words, I don't think the `for` loop has anything to do with what you're seeing (I'm not disputing the evidence, just proposing that it's coincidental and nothing else).

To test my hypothesis, I created the following very simple Windows Forms app:

```
using System;
using System.Threading;
using System.Windows.Forms;

using Timer = System.Threading.Timer;

public class Program
{
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new TestForm());
    }
}

class TestForm : Form
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        var timer = new Timer(TimerProc, null, 1000, 1000);
    }

    public void TimerProc(object state)
    {
        for (int i = 0; i <= 6; i++)
        {
            Console.WriteLine(DateTime.Now.ToLongTimeString());
        }
    }
}

```

I saw the current time print to the console for a little more than thirty seconds; then it stopped.

In contrast, the following change appears to resolve the problem.

```
class TestForm : Form
{
    // Note: declare a field to store a reference to the timer.
    Timer timer;

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.timer = new Timer(TimerProc, null, 1000, 1000);
    }

    // ...
}

```

I started the program again after making the above change, and the timer has continued firing for a few minutes.
Update: After discussion with **Dan Tao** it's probably a GC problem due to the lack of a reference. Adding in the reference fixes the problem [uncomment lines 8 and 20]. The presence of a the 'for loop' in the question probably caused the collection - maybe by generating garbage which explains why removing it and replacing it with a simpler LOC worked for you in this particular instance.

```
using System;
using System.Threading;

namespace SO_TimerTroubles
{
    class Program
    {
        //static Timer t;
        static void Main(string[] args)
        {
            StartTimer();  //Works fine.
            Console.WriteLine("Press Enter to BeginGC");
            Console.ReadLine();
            GC.Collect();  //Timer stops.
            Console.ReadLine();
        }

        public static void StartTimer()
        {
            //t =
            new Timer(TimerProc, null, 1000, 1000);
        }

        static public void TimerProc(object state)
        {
            Console.WriteLine("Timer Called");
            for (int i = 0; i < 5; i++)
            {
            }
        }
    }
}

```

[Original Answer] The following works fine - I suspect the error is elsewhere.

```
class Program
    {
        static void Main(string[] args)
        {

            var timer = new Timer(TimerProc, null, 1000, 1000);

            Console.ReadLine();
        }

        static public void TimerProc(object state)
        {
            Console.WriteLine("Called");
            for (int i = 0; i <= 6; i++)
            {
                Console.WriteLine(i);
            }

        }
    }

```