2

I've been trying to track down a bug I thought was thread-related, but I think instead there is an issue with the way I am using OpenNETCF's Stopwatch. I am using OpenNETCF.IoC in my application, but for the sake of simplicity I moved the following code directly into a view:

public partial class WorkoutView : SmartPart
{
 ...
 private Stopwatch stopwatch; 
 public WorkoutView()
 {  ...
    stopwatch = new Stopwatch();
    stopwatch.Reset();
    stopwatch.Start(); 

    WorkoutDisplayTimer = new Timer();
    WorkoutDisplayTimer.Interval = 500;
    WorkoutDisplayTimer.Tick += new EventHandler(WorkoutDisplayTimer_Tick);
    WorkoutDisplayTimer.Enabled = true;
 }
 void WorkoutDisplayTimer_Tick(object sender, EventArgs e)
 { ...
   stopwatch.Stop();
   lbl.Text = stopwatch.ElapsedTicks.ToString() + "NOT WORKING: " + stopwatch.Elapsed.ToString();
   stopwatch.Start();
  }
  ...
}

Long story short, looking at stopwatch in the debugger, the only values that ever get updated are ElapsedTicks, mElapsed, mStartPerfCount. Everything else is always zero. Is this expected behavior? Do I need to call an additional method to have the stopwatch calculate the Elapsed struct? (Note: stopwatch.ElapsedMilliseconds is also zero)

ctacke
  • 66,480
  • 18
  • 94
  • 155
pithyless
  • 1,659
  • 2
  • 16
  • 31

2 Answers2

2

Yep, it appears to be a bug, specifically in Stopwatch.cs, line 136.

It currently reads:

smFreqInTicks = (MILLIS_IN_TICKS * 1000) / freq;

it should read:

smFreqInTicks = (MILLIS_IN_TICKS * 1000d) / freq;

Right now smFreqInTicks ends up being always zero, which kills the values you're looking at.

ctacke
  • 66,480
  • 18
  • 94
  • 155
  • Thanks for the response! Any chance you're going to make a release with a bugfix sometime soon? I'm currently using the Community SDF so unfortunately I do not have access to the source code to make my own patch. – pithyless Apr 01 '10 at 10:25
1

Why not use the version in the Compact Framework itself? It's in there from version 3.5 onwards...

pyrachi
  • 800
  • 6
  • 15
  • Thanks for pointing this out. Haven't gotten around to testing the CF.Net version yet, but if there are no problems with it I will switch over. – pithyless Apr 07 '10 at 14:33