简体   繁体   中英

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

I have a global var that i'm getting the DateTime.Now once i click a button:

dt = DateTime.Now;

Then inside a progresschanged event i'm doing:

var currentTime = DateTime.Now;

Now i need to calculate the difference between the currentTime and the stored time(dt) in seconds. Then i need to Divide BytesSent by the difference in seconds.

This is the 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 the bottom of the event i need ot make the calculation and this way i can report the 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 and diffSeconds are 0 so i get exception cant divide by zero on the averageSpeed.

Why diffSeconds is 0 ? Tried to calculate the time difference between dt and currentTime.

EDIT

This is what i 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 now on the long averageSpeed cant convert type double to long.

And this is the method that i use to convert to MegaBytes:

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

And in the backgroundworker progresschanged event:

label8.Text = stringProgressReport[4];

How can i show the user something nicer ? for example the speed something like:

The average speed is: 0.5 MB/s then 0.9 MB/s then 1.6 MB/s in this format or something else nicer.

You can simply substract two DateTime objects:

TimeSpan diff = currentTime - dt;
int diffSeconds = (int) diff.TotalSeconds;
var elapedSeconds = (DateTime.Now-dt).TotalSeconds;

You need a little bit of defensive programming to prevent the exception:

var diffSeconds = (DateTime.Now - dt).TotalSeconds;
long averageSpeed = diffSeconds !=0 ? obj.BytesSent / diffSeconds : 0L;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM