CyrusBuilt StopWatch Library for Mono/.NET
I’ve worked on several projects where I desperately needed functionality of a StopWatch in code. For example, I worked on a front-end for nmap. When ever you clicked the “start scan” button to initiate the nmap scan, the idea was to start a timer and then update a label with the elapsed time as the time passed, and then give a total scan duration (yes, nmap calculates scan duration itself, but I wanted the combined duration of the front-end’s operations *plus* nmap’s).
This really isn’t that complicated. The System.Timers namespace provides most of what we need to do exactly that. But I need MORE from the System.Timers class. I needed methods that performed a number of calculations AND built a formatted elapsed time string for me, so I didn’t have to keep repeating this code in an event handler. I have seen examples from people using the TimeSpan class where they would set a variable equal to DateTime.Now before they started an operation, then get the value of DateTime.Now when the operation was over and calculate then calculate the difference. This is all well and good, but I needed something that acted more like a real stopwatch. At a defined interval (or pulse, if you will), I wanted an event to get raised, which simulated the tick of the second hand on the stopwatch. I could then have an event handler that got all the time information for the event and only have to update a label (or whatever) using the data passed from the event.
Yes, System.Timers.Timer does have an “Elapsed” event. But the event doesn’t give me everything I want. So basically, I decided to write my own reusable class library for both Mono and .NET that accomplished everything I needed, and then some.
The CyrusBuilt StopWatch class library has the following features:
- Calculate elapsed time between start time and current time (if running) or start time and stop time.
- Calculate total elapsed time in days.
- Calculate total elapsed time in hours.
- Calculate total elapsed time in minutes.
- Calculate total elapsed time in seconds.
- Calculate total elapsed time in milliseconds.
- Get a formatted elapsed time string.
- Raise an event that passes the above information in the event arguments.
- Start/Stop the stopwatch.
- Allow user defined “pulse” or “tick” interval (defaults to 1 second).
- Set a flag to indicate whether or not the stopwatch is running.
The following is an example of how to use it:
Without events:
using CyrusBuilt.Utils; StopWatch sw = new StopWatch(); sw.Start(); // Wait 3 seconds. while (sw.IsRunning) { System.Threading.Thread.Sleep(3000); } sw.Stop(); Console.WriteLine("Time elapsed: " + sw.GetElapsedTimeString()); sw.Dispose();
With Events
This is a lil bit trickier, but still quite manageable and probably the most common way it would be implemented in a Form. First, let’s create an event handler for the Elapsed event. Assume we have a label control called MyLabel:
using CyrusBuilt.Utils; // put this somewhere inside a class declaration or FormLoad event handler (for example). StopWatch sw = new StopWatch(); // ........ shortened for brevity ..... // private void StopWatchElapsed(object sender, StopWatchElapsedEventArgs e) { MyLabel.Text = e.ElapsedTimeString; }
Now we just need to register the event, then when the event fires after we start the stopwatch the label will be automatically updated with the elapsed time.
private void Go() { sw.Elapsed += new StopWatchElapsedEventHandler(StopWatchElapsed); sw.Start(); while (sw.IsRunning) { System.Threading.Thread.Sleep(3000); } sw.Stop(); }
In the above example, the label text should be “03:00″ (3 seconds, 0 milliseconds). Since we know only a matter of seconds will pass for our operation, we could have also written our event handler like this:
private void StopWatchElapsed(object sender, StopWatchElapsedEventArgs e) { MyLabel.Text = string.Format("Elapsed: {0} seconds", e.ElapsedSeconds.ToString()); }
The StopWatch library is released under GPLv2 and the source code and binaries are available from the downloads page in the “Mono/.NET” section.

Leave a Reply
You must be logged in to post a comment.