Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7600075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:49:19+00:00 2026-05-30T22:49:19+00:00

How can I calculate the speed per sec, and the time left in sec?

  • 0

How can I calculate the speed per sec, and the time left in sec? I’ve tried to use:

void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
    long prevSum = 0;
    while (fileTransfer.busy) {
        rate = (fileTransfer.sum - prevSum);
        RateLabel(rate);  //converting prevSum to (int)KB/SEC
        if (rate != 0)
            left = (fileTransfer.fileSize - fileTransfer.sum) / rate;
        TimeSpan t = TimeSpan.FromSeconds(left);
        timeLeftLabel(FormatRemainingText(rate, t)); //show how much left
        prevSum = fileTransfer.sum;
        Thread.Sleep(1000);
    }
}

but the rate and the time left goes up and down like (30MB/sec then 5MB/sec) permanently.

This is the sendfile code:

public static void sendFile(string filePath) {
    // run the progres Form
    Thread thFP = new Thread(fpRUN);
    fileProgress fP = new fileProgress("Sending...");
    thFP.Start(fP);

    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    string fileName = Path.GetFileName(filePath);
    byte[] fileData;
    try {
        //sending file name and file size to the server
        busy = true;
        fileSize = fs.Length;
        byte[] fileDetial = null;
        string detail =  fileName + "," + fileSize.ToString();
        fileDetial = Encoding.ASCII.GetBytes(detail);
        client.Send(fileDetial);

        //sending file data to the server
        fileData = new byte[packetSize];
        count = 0;
        sum = 0;                          

        fP.SizeLabel(fileSize); // tell the form the file size
        while (sum < fileSize) {
            fs.Seek(sum, SeekOrigin.Begin);
            fs.Read(fileData, 0, fileData.Length);
            count = client.Send(fileData, 0, fileData.Length, SocketFlags.None);
            sum += count;
            fP.ProgressBarFileHandler(sum,fileSize); //progressbar value
            fP.SentLabel(sum);                  //tell the form how much sent
        }
    }
    finally {
        busy = false;
        fs.Close();
        fileData = null;
        MessageBox.Show(string.Format("{0} sent successfully", fileName));
    }
}

How can I fix that? Is there a better way to calculate the speed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:49:20+00:00Added an answer on May 30, 2026 at 10:49 pm

    in form constructor

    Timer timer1 = new Time();
    public Form1()
        {
            InitializeComponent();
            this.timer1.Enabled = true;
            this.timer1.Interval = 1000;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        }
    

    or add it from toolbox and set the previous values

    the sum of sent bytes should be public so our method can get its value every second

        long sentBytes = 0;      //the sent bytes that updated from sending method
        long prevSentBytes = 0;   //which references to the previous sentByte
        double totalSeconds = 0;   //seconds counter to show total time .. it increases everytime the timer1 ticks.
        private void timer1_Tick(object sender, EventArgs e)
        {
            long speed = sentBytes - prevSentBytes ;  //here's the Transfer-Rate or Speed
            prevSentBytes = sentBytes ;
            labelSpeed.Text = CnvrtUnit(speed) + "/S";   //display the speed like (100 kb/s) to a label
            if (speed > 0)                //considering that the speed would be 0 sometimes.. we avoid dividing on 0 exception
            {
                totalSeconds++;     //increasing total-time
                labelTime.Text = TimeToText(TimeSpan.FromSeconds((sizeAll - sumAll) / speed));
                //displaying time-left in label
                labelTotalTime.Text = TimeToText(TimeSpan.FromSeconds(totalSeconds));
                //displaying total-time in label
            }
        }
    
        private string TimeToText(TimeSpan t)
        {
            return string.Format("{2:D2}:{1:D2}:{0:D2}", t.Seconds, t.Minutes, t.Hours);
        }
    
        private string CnvrtUnit(long source)
        {
            const int byteConversion = 1024;
            double bytes = Convert.ToDouble(source);
    
            if (bytes >= Math.Pow(byteConversion, 3)) //GB Range
            {
                return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 3), 2), " GB");
            }
            else if (bytes >= Math.Pow(byteConversion, 2)) //MB Range
            {
                return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 2), 2), " MB");
            }
            else if (bytes >= byteConversion) //KB Range
            {
                return string.Concat(Math.Round(bytes / byteConversion, 2), " KB");
            }
            else //Bytes
            {
                return string.Concat(bytes, " Bytes");
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I calculate speed using Core Location? I would like to use GPS
Can someone explain with an example how we can calculate the time and space
For my application I want to calculate my car/bike speed using iphone. How can
Can someone check this calculation? I want to calculate the speed of my internet
Possible Duplicate: How to calculate speed of our car using iphone How can I
I can calculate the size of the files in a tarfile in this way:
Is there anyway i can calculate the sum of the square of objects in
Hi i was wondering if anyone knows how i can calculate the difference between
I am planning to develop an application that can calculate the limit of an
How can I calculate the value of PI using C#? I was thinking it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.