Time Problem

  • Thread starter Thread starter Xarky
  • Start date Start date
X

Xarky

Hi,
I am creating a program, and when it is executed it stores the
time. Now at each second I will display the duration of the program
execution.

DateTime progExecuted = DateTime.Now;

I started a thread that is to be used only for the timer. In the
thread I have another object of type DateTime, that reads the time
each second.

DateTime curTime = DateTime.Now;


Is there a simple way to reduce both timers and get the duration.


Thanks in Advance
 
No need to have the 2nd
DateTime curTime = DateTime.Now;

You can get the duration by
DateTime.Now.Subtract(progExecuted);

Chester.
 
When I tried that, it won't work. It gives an error -
Cannot implicitly convert type 'System.TimeSpan' to 'System.DateTime'
 
Hi,

What do you mean by it wont work? It will return a timespan. If you
substract two times, it must return the duration, isn't it ?

Thanks,
Chester
 
Bernard Pace said:
When I tried that, it won't work. It gives an error -
Cannot implicitly convert type 'System.TimeSpan' to 'System.DateTime'

Indeed. A duration (TimeSpan) *isn't* a DateTime.

DateTime start = DateTime.Now;
....
DateTime end = DateTime.Now;

TimeSpan duration = end-start;
 
after getting timespan
TimeSpan ts = DateTime.Now.Subtract(dateTimeProgramStarted);
DateTime executedPeriod = new DateTime(ts.Ticks);

it gives you correct Time value, but Date part will not be correct
you'll need to subtract 1 from Day, Month and Year part of
executedPeriod, before displaying/using it.

Maqsood Ahmed
Kolachi Advanced Technologies
http://www.kolachi.net
 
Back
Top