【发布时间】:2015-08-08 05:44:40
【问题描述】:
我试图在我的 C# 下载表单中显示一个剩余时间标签,它类似于 chrome 等浏览器。我已经尝试了以下基于 java 对堆栈溢出问题的相同问题的回答,但它真的很不稳定:Goes to negatives, hours go up and down fast as the minutes etc.
var elapsedTime = DateTime.Now.Second - _startTime.Second;
var allTimeFordownloading = (elapsedTime * e.TotalBytesToReceive / e.BytesReceived);
var remainingTime = allTimeFordownloading - elapsedTime;
TimeSpan time = TimeSpan.FromSeconds(remainingTime);
TimeRemaining.Text = string.Format("Time Remaining: {0} Minutes, {1} Seconds", time.Minutes, time.Seconds);
Progress.Value = e.ProgressPercentage;
DownloadPercentage.Text = string.Format("{0}/100%", e.ProgressPercentage);
if (e.BytesReceived < 1024)
BytesLeft.Text = string.Format("{0}/{1} KBs", Math.Round(e.BytesReceived / 1024f), Math.Round(e.TotalBytesToReceive / 1024f));
else
BytesLeft.Text = string.Format("{0}/{1} MBs", (Math.Round((e.BytesReceived / 1024f) / 1024f)), Math.Round((e.TotalBytesToReceive / 1024f) / 1024f));
_startTime 是在调用 DownloadFileAsync 方法之前开始的 DateTime。 Progress 是我表单上 ProgressBar 的名称,e 是传递给事件处理程序的 DownloadProgressChangedEventArgs 对象。
编辑: 我的问题是计算 C# WebClient 下载剩余时间的最佳方法?
【问题讨论】:
-
如果你能回答这个问题,我们会回答你的问题:当我开车时它看起来是蓝色的。