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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Kirtan PatelPosted Sep 20, 2009, 1:03 AM
By threading you can make your own Timer like Object
Dushan StankovicPosted Sep 20, 2009, 7:36 AM
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.
Master BillaPosted Sep 19, 2009, 12:54 PM
You need timer object to display the real time.
thank you