Results 1 to 4 of 4

Thread: can't stop this timer

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    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.

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405
    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width