-4

What I have:

  1. I'm displaying the current time and date (real-time) at the bottom of a form using a timer element.
  2. I'm using two labels to display the time and date respectively.

What I need:

I need the time and date labels to display as instantly as everything else.

My problem:

There is a two second delay in the displaying of the time and date labels.

My code:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'Format time and date labels.
    TimeMain.Text = Format(Now, "hh:mm:ss")
    DateMain.Text = Format(Now, "dddd, d/MM/yyyy")
End Sub

Note: The above is preceded by a Form_Load sub that simply defines a default accept button. The above is followed by 5 by five short subs.

Edit:

Though the steps for reproducing the problem have already provided in the comments I've been requested to reiterate here. The only difference between the two code blocks posted in this question is that I've left the label text at default to spare the reproducer having to type anything.

Drag two labels and a timer onto a new form and use the following code:

Public Class Form1

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        'Format time and date labels.
        Label1.Text = Format(Now, "hh:mm:ss")
        Label2.Text = Format(Now, "dddd, d/MM/yyyy")
    End Sub

End Class

For the timer's properties, Enabled is defined as True and Interval as 1000.

Clarus Dignus
  • 3,847
  • 3
  • 31
  • 57
  • 2
    What is the interval set to? and what is `Now`? Can you post a minimal sample to reproduce the problem? – Sriram Sakthivel Oct 22 '14 at 18:36
  • So you're not filling in the label's text properties until the first timer tick of your timer? What is its interval? Can you not just create their text values without the timer? – Kritner Oct 22 '14 at 18:36
  • You mean initially it takes two seconds or always two seconds delay? – Sriram Sakthivel Oct 22 '14 at 18:38
  • @SriramSakthivel The interval is at 1000. Now doesn't refer to anything. I'm presupposing this may indicate and error. To reproduce this issue, simply drag two labels onto a new form and use the following code: http://pastebin.com/FGCr1Cg0 (it's the exact same as the code provided in my question but I've left the label names at default to spare you having to type anything). – Clarus Dignus Oct 22 '14 at 19:00
  • 2
    Having all of your questions in this giant font "What I Have", "What I need" format is kind-of annoying. Instead of re-instating the giant font, you could have answered @SriramSakthivel 's question about the interval. It's either that, or you have something blocking the UI thread. Windows is not a real time operating system. – LarsTech Oct 22 '14 at 19:04
  • @Kritner The issue occurs irrespective of whether or not the label's text defined. Interval is 1000. I need the timer because the time/date is real-time as explained in question. – Clarus Dignus Oct 22 '14 at 19:04
  • 1
    @LarsTech If I have the option the accept/decline the edits of others members then surely I should be able to exercise this right without reproach. Furthermore, I can corroborate that other members (some almost as reputable as you) generously applause the format of my questions (see here: http://stackoverflow.com/q/23806386/2971649). The "giant font" is this website's default heading size. It's there to be used. Secondly, it's unfair to assume I wasn't answering Sriram's inquiry. He asked for a minimal sample or reproduction. Allow me more the 10 minutes to fulfill his request. – Clarus Dignus Oct 22 '14 at 19:17
  • Wherever you're starting your timer, just before that set the values. What everyone is saying is that of course the labels aren't being set because the program is waiting 1 second until it sets the text for the very first time. Jeez. And I just saw your latest comment. This isn't your personal blog. It's for the asking and answering of questions, and ridiculous formatting makes that goal more difficult to accomplish. – helrich Oct 22 '14 at 19:19
  • Maybe Java programmers like that formatting? :-) Anyway, you still didn't update the question with the interval information, but even then, you didn't give us enough to answer the question. Post code that duplicates the problem. – LarsTech Oct 22 '14 at 19:22
  • @LarsTech I already have. Review the fourth comment whereupon I provided SriramSakthivel the required code as requested. – Clarus Dignus Oct 22 '14 at 19:25
  • It's lost in the comments. Don't ignore the second, more important part: post code that duplicates the problem. – LarsTech Oct 22 '14 at 19:28
  • you have only provided your timer event handler, what about where your timer is being instantiated? We would get information such as interval, autoReset, etc – Kritner Oct 22 '14 at 19:45
  • I suggest, that you try to reproduce the problem with two labels and a timer. – γηράσκω δ' αεί πολλά διδασκόμε Oct 22 '14 at 19:48
  • @Kritner Thank you for your clarification. I merely dragged a time control onto the form and set Enabled to True and and Interval to 1000 via properties. – Clarus Dignus Oct 22 '14 at 19:49
  • ah... heh it's been such a long since I've used a designer i forgot you could just drag components onto :O – Kritner Oct 22 '14 at 19:51
  • @γηράσκωδ'αείπολλάδιδασκόμε Apologies but I cannot tell if you are directly addressing me or making a universal comment. If you're addressing, I have done so. – Clarus Dignus Oct 22 '14 at 19:52
  • The only advice i can give you is to restart VS and create a new project. – γηράσκω δ' αεί πολλά διδασκόμε Oct 22 '14 at 20:02
  • @helrich The timer is a control dragged onto the form in design mode. I've provided every line of code that I have. My comment you likened to being my "personal blog" is unfair. It's clear to see that I was replying to LarsTech's inquiries on a point-by-point basis as concisely as possible. – Clarus Dignus Oct 22 '14 at 20:06
  • @γηράσκωδ'αείπολλάδιδασκόμε Thank you but I already have (see my last comment to you). The instructions I provided for reproducing the issue (see my question) are based on my creating of a new project and yes, I had restarted Visual Studio. – Clarus Dignus Oct 22 '14 at 20:11

1 Answers1

1

I know this isn't exactly an answer but this is too long to fit in a comment. Also read here for a little more information on timers: Why are .NET timers limited to 15 ms resolution? Does the System.Windows.Forms.Timer run on a different thread than the UI?

Timer elapsed events from what i understand (which could very well be wrong) aren't guaranteed to fire exactly when the time has elapsed, it's more of... put it in queue to fire once the timer has elapsed.

Imagine your application/timer started at "00:00:01.999" and your label states "00:00:01" as the current time.

Exactly 1000 MS later you're at "00:00:02.999 and the elapsed event fires, completing at "2014-01-01 00:00:03.0045" and your label is updated to "2014-01-01 00:00:03" - you've already "lost" a second here.

You could try setting your interval to something lower than one second (say 750) which would get you a potentially more accurate looking counter. Additionally, ensure you're setting the timer labels on form load. I've not worked very much with timers and i'm having trouble finding the article i was reading earlier but you might need to worry about UI locking depending on the timer type used (there are apparently 4 timer classes in the .net framework.) Perhaps someone else can expand on that though, I don't know much about winforms.

Community
  • 1
  • 1
Kritner
  • 13,557
  • 10
  • 46
  • 72
  • Though setting the timer labels on form load produces the label instantly, they are no-longer in real-time (seconds not incrementing). Reducing the interval however, did work. An interval of 100 works. I had thought that the interval would determine in milliseconds how quickly the seconds of my timer would increment but I now understand that the interval refers the firing of the event. Thank you for your persistence and ingenuity. Useful context here: http://msdn.microsoft.com/en-us/library/system.timers.timer.interval%28v=vs.110%29.aspx – Clarus Dignus Oct 22 '14 at 22:59
  • @Kritner : [Timers article by Steve](http://stackoverflow.com/questions/26517177/vb-net-will-a-function-run-if-something-else-is-running#comment41664469_26517177) in this [SO Topic](http://stackoverflow.com/questions/26517177/vb-net-will-a-function-run-if-something-else-is-running) – Karl Stephen Oct 23 '14 at 03:59