1

I am new to C#, and I have searched I but didn't find a simple solution to my problem. I am creating a Windows form application. After the start button is clicked, it counts every millisecond and when it reaches specific values from an array changes a label. How can milliseconds be counted?

-------------------------

AlekZanDer Code:

            namespace timer_simple3
    {
        public partial class Form1 : Form
{
    long result = 0;
    public Form1()
    {
        InitializeComponent();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {

         result = result + 1;
        label1.Text = Convert.ToString(result);

    }

    private void btstart_Click(object sender, EventArgs e)
    {
        timer1.Interval = 1; //you can also set this in the
        //properties tab
        timer1.Enabled = true;
        timer1.Start();
       // label1.Text = Convert.ToString(timer1);
    }

    private void btstop_Click(object sender, EventArgs e)
    {
        timer1.Stop();
    }
}
    }
Jane Abrams
  • 35
  • 1
  • 1
  • 6

2 Answers2

0

First you have to create a method that tells the timer what to do every [put the needed number here] milliseconds.

private void randomTimer_Tick(object sender, EventArgs e)
{
    if (conditions)
    {
        ... //stuff to do
        ... //more stuff to do
        ... //even more stuff to do
    } 
}

Then you set the timer to call this method: you can do this by using the events tab of the properties of the timer or write:

this.randomTimer1.Tick += new System.EventHandler(this.randomTimer1_Tick);

in the ProjectName.Designer.cs file in the private void InitializeComponent(){} method after the line this.randomTimer = new System.Windows.Forms.Timer(this.components);.

And lastly you enable the timer:

private void startButton (object sender, EventArgs e)
{
    randomTimer.Interval = timeInMilliseconds; //you can also set this in the
                                               //properties tab
    randomTimer.Enabled = true;
}

Of course, you will have to set the button to call this method too.

If you don't know where the Properties window is (I assume that you are using Visual C#): it's usually a tab located on the right side of the window. In order something to appear in the tab, you have to select the form you want to edit in the design view. If there is no such tab anywhere in the window of the compiler, go to "View" -> "Other Windows" and select "Properties Window".

If the answers you have found are long and complicated, that's mostly because they are explaining the whole process with details and examples. If you use the "drag and drop" option of Visual C#, the declaration code of the forms will happen automatically, afterwards it's up to you to write the code of the methods. There are also other features that are self explanatory and make programming more pleasant. Use them!

AlexSavAlexandrov
  • 858
  • 2
  • 13
  • 34
0

How can milliseconds be counted?

You can't do that, because Windows Forms Timer component is single-threaded, and is limited to an accuracy of 55 milliseconds. If you require a multithreaded timer with greater accuracy, use the Timer class in the System.Timers namespace.

Also any other timer will not give you accuracy more than 16 milliseconds (actually 15.625 milliseconds, or 64Hz). So, you can't increment some counter to count elapsed milliseconds.

Option for you - instead of long result counter use difference between current time and time of timer start:

label1.Text = (DateTime.Now - startDateTime).Milliseconds.ToString();
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • *BTW* if you really need to use incremental counter - you can use unmanaged Media Timer, like here http://stackoverflow.com/a/13521642/470005 – Sergey Berezovskiy Nov 23 '12 at 06:28