C# 如何使用秒表计算文件下载的下载速度?
在表格1的顶部C# 如何使用秒表计算文件下载的下载速度?,c#,.net,winforms,C#,.net,Winforms,在表格1的顶部 Stopwatch sw = new Stopwatch(); 然后 问题是我从不启动swStopwatch,也从不在任何地方停止/重置它。 因此,在这一行中: label3.Text = string.Format("{0} kb/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00")); 我在西南方向看到了无限的符号 问题是我应该在哪里启动/停止/重置sw 现在,当我运行程序时,我在
Stopwatch sw = new Stopwatch();
然后
问题是我从不启动swStopwatch,也从不在任何地方停止/重置它。
因此,在这一行中:
label3.Text = string.Format("{0} kb/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00"));
我在西南方向看到了无限的符号
问题是我应该在哪里启动/停止/重置sw
现在,当我运行程序时,我在label3上看不到任何东西,我想是因为无穷大,我没有启动swStopwatch。在开始下载之前启动Stopwatch是有意义的,因为如果你更早地启动它,会产生错误的结果
private async Task DownloadFile()
{
if (_downloadUrls.Any())
{
WebClient client = new WebClient();
[...]
sw = Stopwatch.StartNew();
await client.DownloadFileTaskAsync(new Uri(url), @"C:\Temp\TestingSatelliteImagesDownload\" + count + ".jpg");
return;
}
}
并在下载被取消或完成后停止
// The event that will trigger when the WebClient is completed
private async void Completed(object sender, AsyncCompletedEventArgs e)
{
if ([...])
{
[...]
}
else
{
[...]
}
sw.Stop();
}
你还想在这之前阻止它returning@slawekwin哪里在这个事件中?你能告诉我还有什么地方可以停吗?
private async Task DownloadFile()
{
if (_downloadUrls.Any())
{
WebClient client = new WebClient();
[...]
sw = Stopwatch.StartNew();
await client.DownloadFileTaskAsync(new Uri(url), @"C:\Temp\TestingSatelliteImagesDownload\" + count + ".jpg");
return;
}
}
// The event that will trigger when the WebClient is completed
private async void Completed(object sender, AsyncCompletedEventArgs e)
{
if ([...])
{
[...]
}
else
{
[...]
}
sw.Stop();
}