Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
HI...
I want to display the current time on a listview, this time automatically changes with the clock running Windows...
Posted
Comments
Sandeep Mewara 19-Apr-11 4:53am    
And your effort is?

Using any kind of timer will give you more trouble than a thread — highly recommended.
Just to give you an idea: did you plan for situations when your timer event handler is not yet exited at the moment the next one if fired? There are many other traps. The worst one is System.Windows.Forms.Timer.

Create a thread infinitely running in a loop, using System.Threading.Thread.Sleep for the required amount of time. Your can trigger any UI operation based on this repetition, but remember, you cannot call anything on the UI directly from this non-UI thread. Instead, you should use the methods Invoke or BegingInvoke of System.Threading.Dispatcher or System.Windows.Forms.Control (absolutely does not matter what instance of control, it should be any control really participating in you UI, for example, your form). You will find very detailed explanation of how it works and further directions in my past Answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

Just in case: my collection of references to my past Answers on threads, could be very useful:
How to get a keydown event to operate on a different thread in vb.net[^].

—SA
 
Share this answer
 
v2
Comments
guendouz bachir 18-Apr-11 15:03pm    
thank you, but i work with VC# in WFA not the console mode (CA)...
Sergey Alexandrovich Kryukov 18-Apr-11 16:40pm    
What are your talking about?! Who tells you about console mode?!

This is all about using thread in UI -- read properly!

--SA
Espen Harlinn 18-Apr-11 17:44pm    
5ed!
Sergey Alexandrovich Kryukov 18-Apr-11 22:15pm    
Thank you, Espen.
--SA
Just use a System.Timers.Timer object, and in the tick event handler, update your listview.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Apr-11 14:17pm    
This is correct, good it is not a Forms timer. My 5, but...

How about our usual recommendation to use thread instead of timer?
It did not give up, please see my Answer.
--SA
#realJSOP 18-Apr-11 14:55pm    
I didn't feel like writing the code - again. I freakin' hate Timer objects, but very few share the senitment, especially new programmers who see "the easy way", use it, and then bitch/moan because they don't really have any control over what's happening (and they realize that the timer tick event is blocked/non-responsive if it fires too often during long processes).
Sergey Alexandrovich Kryukov 18-Apr-11 16:39pm    
At to me the writing code, I reuse my old Answers. I share all your hate of timers. Those thinking the "the easy way" simply don't know what's easy, I observed the frustration for lost time before my eyes. Saying "you see, I told you!" was a bit late, the bitter lesson already learned. In some cases, it was a good amount of wasted time from the company project.
--SA
Sergey Alexandrovich Kryukov 18-Apr-11 16:42pm    
Just got a reply from OP: "I work [...] not the console mode". No use! just a waste of time!
--SA
Insert a Timer in form and double click on timer icon and in Timer function write this code :
note : set enable timer and set interval 100.
This a timer function :

private void timer1_Tick(object sender, EventArgs e)
       {
           Label.Text = DateTime.Now.ToLongTimeString();
       }
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Apr-11 14:18pm    
This is the worst of timers, you would better recommend System.Timers.Timer.
Showing of DateTime is good. So, my 4.
Thread should be preferred over timer! Please see my answer.
--SA
guendouz bachir 18-Apr-11 14:21pm    
I want it still works,exactly with windows7 time ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900