There's a subtle bug hidden in your code.
int hashCode = Math.Abs(this.GetHashCode());
When Math.Abs
is passed int.MinValue
, it throws an OverflowException
:
Negating the minimum value of a twos complement number is invalid.
GetHashCode
defers to Guid
's GetHashCode
, which makes no guarantees that it won't return int.MinValue
.
How unlikely is it? Well I've had a program running for fifteen minutes now to find a Guid
with a hash code of int.MinValue
and I've only found one, but it can happen and it's good to be aware of (and fix!).
One neat way to fix the bug would be
int hashCode = this.GetHashCode() & int.MaxValue;
that is, assuming you don't really care about the value returned by GetHashCode
, you just want some non-negative integer.
This loop
while (!_shouldStop) { long l = c.ElapsedTicks; if ((l % this.RandomStop) == 0) { OnTickedEvent(l); } }
pegs the CPU. Two metronomes increases my CPU usage from ~10% to 50%; four metronomes takes it up to 100%. Take a look at System.Timers.Timer
for a way of getting the same sort of functionality without using up all the CPU.
I would strongly recommend against using #define DEBUG
and #define TRACE
in your files like this. To compile without them would require going through every file in your project and removing those lines.
These symbols can be set in Visual Studio at a project level in the project build settings:
Alternatively you can set them using the /define
option if you're compiling by hand.