i'm trying to display the time in seconds in a label on a form.
i'm trying to loop until the timer has stopped but when i do try and stop the timer the app takes no notice and carries on executing the loop and eventually the app does not respond and i have to kill it.
this is the code from the form. i assume its something to do with the code executing on another thread. or something.
Code:private void button1_Click(object sender, System.EventArgs e) { aTimer.Start(); while(Stopped == false) { label1.Text = aTimer.Duration.ToString(); } } private void button2_Click(object sender, System.EventArgs e) { aTimer.Stop(); Stopped = true; label1.Text = aTimer.Duration.ToString(); }
the timer i'm using this is class that i found somewhere on the net
that should produce a hi resolution timer. (cos i want to create a stopwatch like application).
any help would be appreciated.Code:using System; using System.Runtime.InteropServices; using System.ComponentModel; using System.Threading; namespace HighResTimer { public class HighResTimer { [DllImport("Kernel32.dll")] private static extern bool QueryPerformanceCounter(out long lpPerformanceCount); [DllImport("Kernel32.dll")] private static extern bool QueryPerformanceFrequency(out long lpFrequency); private long startTime ; private long stopTime; private long freq; /// <summary> /// ctor /// </summary> public HighResTimer() { startTime = 0; stopTime = 0; freq =0; if (QueryPerformanceFrequency(out freq) == false) { throw new Win32Exception(); // timer not supported } } /// <summary> /// Start the timer /// </summary> /// <returns>long - tick count</returns> public long Start() { QueryPerformanceCounter(out startTime); return startTime; } /// <summary> /// Stop timer /// </summary> /// <returns>long - tick count</returns> public long Stop() { QueryPerformanceCounter(out stopTime); return stopTime; } /// <summary> /// Return the duration of the timer (in seconds) /// </summary> /// <returns>double - duration</returns> public double Duration { get { return (double)(stopTime - startTime) / (double) freq; } } /// <summary> /// Frequency of timer (no counts in one second on this machine) /// </summary> ///<returns>long - Frequency</returns> public long Frequency { get { QueryPerformanceFrequency(out freq); return freq; } } } }


Reply With Quote