A Clock User Control for your Windows Form in C#(mycsharpcorner.com)

submitted by youseflayousefla(675) 5 years, 1 month ago

Making your own Clock control to display on your Windows Form to display and update the current time up to the second is an easy deal using C# Timers. Creating The Clock User Control Step By Step Here is one way to do it:

1 comment |category: |Views: 479

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by yesthatmcgurkyesthatmcgurk(4063) 5 years, 1 month ago 0

Wow. Ten times a second this control will get the current time and convert it to a long time string. TEN TIMES A SECOND. Ten times a second DateTime.Now() will create a new... no, make that a minimum of three and a maximum of five (and maybe more) DateTime objects, a TimeSpan object, and numerous others in a call stack at least six levels deep, all to do what one DateTime object could be used to do, only once a second. Wow.

A better solution would be to use static timer and DateTime objects. Upon init, set the timer to an interval of 1000 ms. Upon the first firing of the tick event (_currentTime == null) set the var to DateTime.Now (_currentTime = DateTime.Now). Read out the current time. Upon every other tick event, increment _currentTime one second and then update the display. It won't be exact, but it'll be damn close and a whole lot less wasteful of resources.
Other cheap improvements: Give your callers the ability to set the interval (show seconds? minutes? hours? days?). Use another timer to sync _currentTime to DateTime.Now every hour. Stuff like this makes your code much less offensive and adds value with little effort.

Reply

information Login or create an account to comment on this story