1秒あたりの速度と残り時間を秒単位で計算するにはどうすればよいですか?私は使用しようとしました:
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);
}
}
ただし、レートと残り時間は、永続的に(30MB/秒から5MB/秒)のように上下します。
これはsendfileコードです:
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));
}
}
どうすれば修正できますか?速度を計算するためのより良い方法はありますか?