Stopwatch- Best approach
I am writing an app to track elapsed time for bike racer. I can start a stopwatch OK, in a window that displays overall time and elapsed time. I want to be able to open a child window to enter individual ride rs numbers and then capture the elapsed time for that rider by pressing a button or Enter key. Is there a way to grab the stopwa tchs elapsed time from one window to another? I tried setting up a class that used the stopwatch but I still have to do a Stopwatch sw = new stopwatch() in the different windows which, of course, gets me a new one. Is there a way to create a unveral or global stopwatch?
AlanPosted Sep 11, 2008, 5:02 AM
You can create a global stopwatch by adding a static field to the Program class (in program.cs) which contains your Main() method:
static class Program
{
internal static System.Diagnostics.Stopwatch Stopwatch = new System.Diagnostics.Stopwatch();
[STAThread]
static void Main()
{
// blah
}
}
You can now easily reference the Stopwatch from anywhere in the application. For example, to start it:
Program.Stopwatch.Start();