0

The timer in my opinion is not very accurate when it displays time from its _Tick() Method. I am wanting to show elapsed time in minutes/seconds show the length of time a procedure takes to complete. I have used this with the Timer but have discovered it's calculations are incorrect. Which is why I wanted to ask would a StopWatch be better to show more accurately or is their a separate control that I should be using altogether?

private int timepassed = 0;
private void buttonFourteen_Click(object sender, DragEventArgs e)
{
  timer2.Start();
  var backgroundWorker = new BackgroundWorker();
  backgroundWorker.DoWork += (s, e) =>
  {
    //Lengthy Procedure Goes Here
  };
  backgroundWorker.RunWorkerCompleted += (s, e) =>
  {
    timer2.Stop();
  };
  backgroundWorker.RunWorkerAsync();
}
private void timer2_Tick(object sender, EventArgs e)
{
  timepassed++;
  timedisplay.Text = timepassed.ToString();
}
Big Pimpin
  • 427
  • 9
  • 23
  • 3
    `show the length of time a procedure takes to complete` you want a stopwatch. A Timer is more for performing an action at set intervals. – Jonesopolis Apr 07 '15 at 18:27
  • 1
    Use the appropriate tool for what you want to do. A `Stopwatch` is used to measure elapsed time. A `Timer` is used for scheduling. At least you aren't using `DateTime` for measuring, since that is also not intended for precise interval measurements. – Rufus L Apr 07 '15 at 18:29
  • I'd use a timer and save the start time, to calculate time difference by `Now - Start`. Stopwatch is needed for accuracy when TESTING performance, rather than displaying a timer of 2decimal points accuracy at best. – SimpleVar Apr 07 '15 at 18:32

2 Answers2

2

System.Diagnostics.StopWatch is the typical method for tracking this sort of thing. If you are using VS and are just trying to do performance checks, you could also profile the code while will give you a good idea of how much time is spent in each method.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
Travis
  • 689
  • 5
  • 11
  • Yes, I am using VS2010 -- How would I profile the code? – Big Pimpin Apr 07 '15 at 18:33
  • 1
    Essentially you start up your application, get it close to the state that will hit the effected code, then in VS go to Analyze, Profiler, Attach/Detach. See the beginner's reference material on the VS profiler here: https://msdn.microsoft.com/en-us/library/ms182372.aspx – Travis Apr 07 '15 at 18:40
2

Here's one way to accomplish this with a stop watch, which should be very accurate:

private readonly Stopwatch sw = new Stopwatch();
private readonly System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

public Form1()
{
    InitializeComponent();
    timer.Tick += timer2_Tick;
}

private void buttonFourteen_Click(object sender, EventArgs e)
{
    sw.Restart();
    timer.Start();
    var backgroundWorker = new BackgroundWorker();

    // Simulating a 10-second process for testing
    backgroundWorker.DoWork += (s, ea) => Thread.Sleep(TimeSpan.FromSeconds(10));

    backgroundWorker.RunWorkerCompleted += (s, ea) => timer.Stop();
    backgroundWorker.RunWorkerAsync();
}

private void timer2_Tick(object sender, EventArgs e)
{
    timedisplay.Text = sw.Elapsed.ToString("g");

    // Or use a custom time format (you can specify precision as well as
    // delimiters between days, hours, minutes, seconds, and milliseconds):
    // timedisplay.Text = sw.Elapsed.ToString(@"dd\.hh\:mm\:ss\.ff");
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43