Reputation: 149
I am new to threading and do not know anything about how it should be used. I am having trouble with my clock. My clock does not give me correct local time, instead gives me about 10-15 minutes delayed time.
I think there is something wrong or missing with my code:
private void MainWindow_Load(object sender, EventArgs e)
{
new System.Threading.Timer((state) =>
{
if (!label2.IsHandleCreated)
return;
BeginInvoke((Action)delegate()
{ label2.Text = " " + DateTime.Now.ToString("hh:MM:ss") + " " +
DateTime.Now.ToShortDateString(); });
}, null, 1000, 1000);
}
I have read that begininvoke should always be paired with endinvoke. But how do I do it?
Edit: I need to know why this code give me delayed time like now its 12:23 here but the program shows 12:03, it always starts at 12:03 when I re-run it. Also sometimes the clock stops. Why?
Upvotes: 1
Views: 345
Reputation: 1285
Your format string hh:MM:ss
is showing you hours (12 hour clock) : month : seconds. You probably intended hh:mm:ss
See Custom Date and Time Format Strings on MSDN for a detailed description.
Upvotes: 1