Your whole approach is too bad to consider particular problems. First of all, it reflects the very, very bad trend notorious in many beginners these days: attempting to work with strings representing data instead of data itself. The timer cannot be "running in a correct time format". Timer has nothing to do with the format.
You need to throw out your code, never do such things again, and start from scratch. Here is what you need:
First, understand the difference between time (a point in a time scale expressed by
System.DateTime
and time duration or interval, expressed by
System.TimeSpan
— this is the type you have to use:
http://msdn.microsoft.com/en-us/library/system.timespan%28v=vs.110%29.aspx[
^].
Now, you need to make sure you are not using the timer type
System.Windows.Forms.Timer
. This type is the easiest to use but totally unsuitable for any action supposed to be more or less periodic, due to prohibitively low time accuracy of this timer. But even with other timer types, you should not rely on its periodic behavior and count the events as you do. Instead, in the handler of your timer event, you should take the real time value from the system. In your case, the most accurate way of doing so is using the class
System.Diagnostics.Stopwatch
:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.110%29.aspx[
^].
Its usage is apparent from the documentation; it give you the values of
TimeSpan
. It allows you to calculate the time from start of the counting.
And only after all these simple calculations are done, you need to present the time span you need to show in you UI in a string. The remaining problem is the notification of your UI (too bad you did not tag what you are using). Let me assume you are making a windows application. Here is a little problem: you cannot call anything related to UI from non-UI thread, and the thread running the handler of your timer event is not the UI thread. 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[
^].
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[
^].
—SA