title: How to display the time elapsed in a label
body: <p>What should be straight forward is not here and I couldnt find a way yet in spite of reading a lot.</p>
  
  <p>I have a button which executes a time consuming function. So on clicking the button should show time elapsed in milliseconds in a label with an interval of 500 ms. And when the desired result is achieved I want the timer to stop. <strong>I dont just need the final time (the total time consumed) in a label, but the label should dynamically show the time being elapsed.</strong> My code would be: </p>
  
  <pre><code>    private void btnHistory_Click(object sender, EventArgs e)
      {
          Class1 c = new Class1();
          c.StartClock(ref label12);
  
          Utility.PopulateHistory(dgvRecords_history, _util); //time consuming function
  
          c.StopClock();
      }
  </code></pre>
  
  <p>And in <code>Class1</code> I write this:        </p>
  
  <pre><code>    internal void StartClock(ref Label l)
      {
          Timer t = new Timer();
          t.Interval = 500;
          t.Enabled = true;
          t.Tag = l;
          t.Tick += new EventHandler(t_Tick);
          t.Start();
      }
  
      int i;
      bool stop;
      void t_Tick(object sender, EventArgs e)
      {
          if (stop)
          {
              ((Timer)sender).Stop();
              return;
          }
  
          ((Label)((Timer)sender).Tag).Text = (++i).ToString();
      }
  
      internal void StopClock()
      {
          i = 0;
          stop = true;
      }
  </code></pre>
  
  <p>What happens is, the <code>t_Tick</code> event is fired only after the complete code under button event is fired. That is the tick event is fired after it goes through the StopClock function! I got no idea why on earth it should be that!</p>
  
  <p>2 questions basically:</p>
  
  <ol>
  <li><p>How can my requirement be achieved in the right way to handle these? I know I should use other built in classes to evaluate performance, but this is just for display purpose. For this, what is the ideal approach?</p></li>
  <li><p>Why is my code not working?</p></li>
  </ol>
  
  <p>EDIT: I have used here <code>System.Windows.Forms Timer</code> here, but the result is not any different with <code>System.Timers Timer</code></p>