Real Time Display
My form displays a clock in the corner. I have a timer object, the clock updates on tick. Is there an alternate way to do this where it just displays the current time all the time without being called from the timer tick?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Kirtan PatelRank #292 · 8.6K reputationPosted Sep 19, 2009, 9:03 PM EDT
By threading you can make your own Timer like Object
Master BillaRank #2,229 · 630 reputationPosted Sep 19, 2009, 8:54 AM EDT
You need timer object to display the real time.
thank you
Dushan Stankovic72 reputationPosted Sep 20, 2009, 3:36 AM EDT
Kirtan is right, with threading namespace and Timer class you can create your own clock. I do one on speed and look something like this:
This sample demonstrate concurrent programming using Timer
You must create Label and put on the form - Name: label1,
and must create field of System.Threading.Timer class - private System.Threading.Timer t;
//in constructor of your main class put this
CheckForIllegalCrossThreadCalls = false; //this is not a good way to use cross thread reference because is not thread safe but is the faster way
t = new System.Threading.Timer(this.timerFunc, this.label1, 0, 1000); // Create and start timer thread witch on interval of 1000 milisec call callback function this.timerFunc
//Timer function will be called in a separate thread by the timer object
private void timerFunc(object sender)
{
Label lbl = sender as Label;
lbl.Text = DateTime.Now.ToShortTimeString();
}
Description: With the Timer class constructor create and start interval thicking witch call timer function each time and all that is working in separate thread. We pass object of the label1 to put into the Text prop value of the current DateTime time.
This way like I said is not a good way because of usage cross thread reference of the object label1. This reference is created in the Main thread and we are using it directly in our new thread. Better way is using BackgroudWorker class with asynhronous call, or Invoke method of a delegate witch is litle complicated way.