Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm Amator...guide me to make a digital clock program in c#! thanks!
Posted

Great! Excellent exercise for a beginner, almost a tradition!

First, decide what you want to use, System.Windows.Forms or WPF. Learn the basics of each (use Google but at first prefer the search parameter "site:Microsoft.com". One hint: use thread, not a timer and, in the thread body also use System.DateTime. Even with timer, always take time from this structure, never rely on timer's timing. (But avoid a timer, a thread is much easier, safer and more correct, in general; make thread body a cycle calling System.Threading.Thread.Sleep with the sleep time less then a second.

What else? Oh, invocation! You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

Same thing with timer. Better use System.Timers.Timer, and don't use System.Windows.Forms.Timer (almost never); it's unreliable in terms of accuracy but does not require invocation, already works in UI thread. For other timers, you need invocation on UI thread, as described above.

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

This beginner's work nevertheless will give you a good chunk of experience in very important .NET techniques you really want to use in most of other projects.

—SA
 
Share this answer
 
v2
Comments
Simon Bang Terkildsen 19-Oct-11 12:09pm    
thorough, +5!
Sergey Alexandrovich Kryukov 19-Oct-11 12:13pm    
Thank you, Simon. At least OP will see what's involved. As I say, the exercise itself is quite useful for learning several key things at one, on some level of learning.
--SA
Sander Rossel 19-Oct-11 13:12pm    
Wow, that is quite the answer, and a good one at that! My 5. Although I am not sure if this is the level the OP is currently at (but that is just an assumption based on the fact that the OP did not have a more detailed question). Using threads is not easy. Although you have provided enough reference material, so the OP should probably manage.
Sergey Alexandrovich Kryukov 19-Oct-11 13:20pm    
Thank you. This way or another, this can be useful. If OP is not ready for the task, at least he will know what's involved. OP is not the only one reader of it, by the way. Interfaces are much easier to handle than many think, if the developers just understands few basic ideas well.
--SA
Sander Rossel 19-Oct-11 13:24pm    
That is true. Anyone with some basic knowledge should be able to make a nice clock with this information :)
People should really learn how to use a search function...
Multipurpose Digital Clock Control using .NET[^]
A WPF Digital Clock[^]

Not the best articles according to their rating, but it's probably the best answer you're going to get.
For any SPECIFIC problems you can come back to the quick answers section...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Oct-11 12:15pm    
Those are good codes for samples, my 5, but for the purpose I would suggest OP would do all the job independently first. Reading ready-to-use solution does not really help much before a students does it which her/his own hands.
--SA
Sander Rossel 19-Oct-11 13:09pm    
True, although I have always learned the most from reading others' code. I couldn't think up something I did not yet know ;)

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