-
can't stop this timer
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).
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;
}
}
}
}
any help would be appreciated.
-
Why are you using that timer? The framework has two timers you can use.
Anyway where is the variable Stopped declared? It might have something to do with that.
-
thanks for the reply
i'm using the highrestimer class because i need a high resolution timer. the app that i plan to use this in will have a stopwatch in it so i need to keep displaying the time elapsed.
Stopped is declared as a class variable of the form as such
Code:
private bool Stopped;
if anyone can help i would really appreciate it. this seems like such a simple thing that i am trying to do.
1) start the timer
2) whilst timer is running constantly/frequently get the elapsed time and display it in a label(textbox/whatever)
3) stop the timer and display the final elapsed time
-
if anyone can help i would really appreciate it. almost all the examples i've found of using timers just use the timer control or something equally inaccurate.
I know this class works i just don''t know how to call the duration in such a way that it updated the label with the duration and knows when i have clicked the stop button