title: C# Timer Callback to use returned value from a function every loop
body: <p>I want to achieve something that i don't know if its possible, but there goes nothing...</p>
  
  <p>i have a static  int variable called X, and a function that increments X then returns the new value.</p>
  
  <pre><code> public static int X;
  
   public int ReturnInt()
      {
          X = X + 1
          return x;
      }
  </code></pre>
  
  <p>i have another function which is a TimerCallBack function that will be looped over every N milliseconds.</p>
  
  <pre><code>public static void TimerCallback(Object o)
      {
          y = ReturnInt();
          Console.WriteLine("Value:" + y);
          GC.Collect();
      }
  </code></pre>
  
  <p>then in the main function:-</p>
  
  <pre><code>public static void Main(string[] args)
      {
  
  
          Timer t = new Timer(TimerCallback, null, 0, X * 1000);
          Console.ReadLine();
      }
  </code></pre>
  
  <p>This application only works on the first loop because it can only see the incremented value of X on the first loop of the Timer then remains constant without updating.</p>
  
  <p>so the output is:</p>
  
  <pre><code>Value:1
  </code></pre>
  
  <p>what i want to do is use the Timer function's last parameter to multiply X's value (as it changes everytime the loop is over) with the milliseconds.</p>
  
  <p>is this even possible?</p>