C# Creating a Timer that at a specific values changes a label

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 5599 Views - Last Post: 22 November 2012 - 05:51 PM Rate Topic: -----

#1 janeabrams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-November 12

C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 01:11 PM

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 this be done?
Is This A Good Question/Topic? 0
  • +

Replies To: C# Creating a Timer that at a specific values changes a label

#2 janeabrams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-November 12

Re: C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 01:19 PM

The question: How can milliseconds be counted?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,380
  • Joined: 02-June 10

Re: C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 01:27 PM

Your plan is WAY off.

If you want to use a timer that's fine. Drag a timer from the toolbox to your form. Set the interval to the number of milliseconds you want. Then assign a handler method to the timer's .Tick event.

Example and description in the MSDN:
http://msdn.microsof...orms.timer.aspx

Don't try to count milliseconds with a timer. It will go way off. System.Forms.Timer is not considered particularly accurate. The .Tick event will fire after a minimum of the set .Interval milliseconds.

In reality that can mean a little drift. If you are using a timer for 10 seconds and are off by 1 or 2 milliseconds its generally not a big deal. But if you are counting milliseconds by 1's, and each one has a drift of 1ms - then you could be off by as much as double.

Let's cut to the chase: What is you are trying to code? Not what approach are you trying to take. What are you trying to accomplish with your program?
Was This Post Helpful? 1
  • +
  • -

#4 janeabrams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-November 12

Re: C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 01:34 PM

I want to loop a 2d array where one column is the time and the second column is a message.
And when the elapsed time is equal with the time value from the array then it will show the message.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,267
  • Joined: 05-May 12

Re: C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 01:38 PM

In that case, configure the timer to elapse at the expected elapsed time. When that event fires, display the message and then reconfigure the timer to be the next elapsed time.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,380
  • Joined: 02-June 10

Re: C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 02:01 PM

A message? Are you trying to create a chat application? Because this type of timed polling when out in the 1990's.

Where is the message coming from? Rather than a timed polling design like this you really want to go with reacting to events if possible. When the message comes in it should raise an event then you react to that however you want, such as displaying it.

Chat tutorials:

[*]Q: ...make a chat or client-server application?
A:Client/server chat tutorial
Peer-to-peer chat

Events tutorial:
Quick and easy custom events

View Postjaneabrams, on 22 November 2012 - 02:34 PM, said:

I want to loop a 2d array where one column is the time and the second column is a message.
And when the elapsed time is equal with the time value from the array then it will show the message.


Ohhh... Is this supposed to be more of an alarm clock or reminder system?
So at 2:45pm a message comes up that says "Take your medication" and so on?

if so then a timer isn't exactly what you want because it counts from its start, not looking at the current time. You could create a timer that ticks every 60 seconds or so then you check the DateTime.Now to see if its approximate to the time for that message. You'll need to build it up so it doesn't show the same message twice in the same minute.
Was This Post Helpful? 0
  • +
  • -

#7 janeabrams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-November 12

Re: C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 02:15 PM

It is not a chat application, it just shows a message when the time elapsed from start of the application reaches the time value from the array.

namespace timer_simple2
{
    public partial class Form1 : Form
    {

        int milli = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            milli++;
            label1.Text = milli.ToString();
            int seconds = milli / 60;
            label2.Text = Convert.ToString(seconds);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }
    }
}



Was This Post Helpful? 0
  • +
  • -

#8 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1020
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 02:36 PM

View Postjaneabrams, on 22 November 2012 - 02:15 PM, said:

            int seconds = milli / 60;

This isn't remotely close to how many milliseconds there are in a second. What does 'milliseconds' mean?
Was This Post Helpful? 1
  • +
  • -

#9 tlhIn`toq  Icon User is online

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,380
  • Joined: 02-June 10

Re: C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 02:52 PM

View PostMomerath, on 22 November 2012 - 03:36 PM, said:

View Postjaneabrams, on 22 November 2012 - 02:15 PM, said:

            int seconds = milli / 60;

This isn't remotely close to how many milliseconds there are in a second. What does 'milliseconds' mean?


Here's a hint: What fraction of a meter is a millimeter?

Its the same metric system, just applied to a second.
  • nanometer
  • millimeter
  • centimeter
  • meter
  • kilometer


You can do the same with seconds:
  • nanosecond
  • millisecond
  • centisecond
  • second
  • kilosecond

Its just that millisecond is the only common form of this.

This post has been edited by tlhIn`toq: 22 November 2012 - 02:54 PM

Was This Post Helpful? 0
  • +
  • -

#10 janeabrams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-November 12

Re: C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 03:50 PM

I tried 3 different methods and they give similar results.
The result is not in milliseconds, but no one can do it in milliseconds.
Was This Post Helpful? 0
  • +
  • -

#11 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1020
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 04:19 PM

No one can do what in milliseconds? Display a message after x milliseconds? Trivial, answer has already been given. Tell how many milliseconds have passed since a specific time, again trivial, check the documentation on MSDN.

We don't solve your problems for you. You write the code, we point out why it does do *what you tell us you want it to do*. We give hints and sometime outright directions. You won't learn a thing if we tell you the answer.
Was This Post Helpful? 0
  • +
  • -

#12 janeabrams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-November 12

Re: C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 04:34 PM

Your answers pointed me to the code bellow. Is that code correct? Does it count milliseconds?
namespace timer_simple2
{
    public partial class Form1 : Form
    {

        int milli = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            milli++;
            label1.Text = milli.ToString();
            

        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }
    }
}





Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is online

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,380
  • Joined: 02-June 10

Re: C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 04:55 PM

This looks approximately right without running it.
But there are MANY problems you will encounter with this.

The first being that the GUI won't react with millisecond quick response time. The label is not capable of updating every 1 millisecond.

The second being that this is going to be so CPU intensive that you aren't going to get anything else done in your application. It is going to be totally bogged down by this one activity every 1 milliseconds.

In other words, to repeat myself but more bluntly this time:
Your design logic of counting milliseconds is not going to work and is a ridiculous idea.

We've tried to point you to better ideas and better ways, but you don't want to try them. You've been told why counting milliseconds using a Forms.Timer is grossly inaccurate, but you're still trying.


I wish you the best of luck with your project but I'll be unsubscribing now as it seems giving you advice is like talking to the wind.
Was This Post Helpful? 0
  • +
  • -

#14 janeabrams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-November 12

Re: C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 05:04 PM

The result isn't in miliseconds and can't get better that this
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq  Icon User is online

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,380
  • Joined: 02-June 10

Re: C# Creating a Timer that at a specific values changes a label

Posted 22 November 2012 - 05:13 PM

View Postjaneabrams, on 22 November 2012 - 06:04 PM, said:

The result isn't in miliseconds and can't get better that this


Huh? What are you trying to say because that's virtually meaningless to anyone not inside your skull.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2