Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用秒表计算文件下载的下载速度?_C#_.net_Winforms - Fatal编程技术网

C# 如何使用秒表计算文件下载的下载速度?

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 现在,当我运行程序时,我在

在表格1的顶部

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();
}