c# - How can i calculate the difference between a stored DateTime.Now and a current DateTime.Now? -


i have global var i'm getting datetime.now once click button:

dt = datetime.now; 

then inside progresschanged event i'm doing:

var currenttime = datetime.now; 

now need calculate difference between currenttime , stored time(dt) in seconds. need divide bytessent difference in seconds.

this progresschanged event i'm using:

double mbsent = 0;         int percentcomplete = 0;         static string progress = "";         static string ttt = "";         private void videosinsertrequest_progresschanged(iuploadprogress obj)         {             stringprogressreport[1] = obj.status.tostring();             backgroundworker1.reportprogress(0, 1);             mbsent = ((double)obj.bytessent) / (1 << 20);             stringprogressreport[2] = mbsent.tostring();             backgroundworker1.reportprogress(0, 2);             percentcomplete = (int)math.round(((double)obj.bytessent) / totalbytes * 100);             stringprogressreport[3] = percentcomplete.tostring();             backgroundworker1.reportprogress(0, 3);             var currenttime = datetime.now;          } 

at bottom of event need ot make calculation , way can report average speed. file upload speed.

edit

i'm getting exception when doing:

var currenttime = datetime.now; timespan diff = currenttime - dt; int diffseconds = (int)diff.totalseconds; long averagespeed = obj.bytessent / diffseconds; 

both obj.bytessent , diffseconds 0 exception cant divide 0 on averagespeed.

why diffseconds 0 ? tried calculate time difference between dt , currenttime.

edit

this did now:

if (obj.bytessent != 0)             {                 var currenttime = datetime.now;                 timespan diff = currenttime - dt;                 var diffseconds = (datetime.now - dt).totalseconds;                 long averagespeed = diffseconds != 0 ? obj.bytessent / diffseconds : 0l;                 double mbunits = convertbytestomegabytes(averagespeed);                 stringprogressreport[4] = mbunits.tostring();                 backgroundworker1.reportprogress(0, 4);             } 

but getting error on long averagespeed cant convert type double long.

and method use convert megabytes:

static double convertbytestomegabytes(long bytes)         {             return (bytes / 1024f) / 1024f;         } 

and in backgroundworker progresschanged event:

label8.text = stringprogressreport[4]; 

how can show user nicer ? example speed like:

the average speed is: 0.5 mb/s 0.9 mb/s 1.6 mb/s in format or else nicer.

var elapedseconds = (datetime.now-dt).totalseconds; 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -