gpt4 book ai didi

c# - 异步下载时设置用户状态

转载 作者:太空狗 更新时间:2023-10-29 22:18:07 24 4
gpt4 key购买 nike

VS 2008 SP1

我正在使用网络客户端异步下载一些文件。

我有 5 个文件要下载。

但是,我想监控每次下载并想将用户状态设置为文件的名称,所以在 ProgressCompletedEvent 中我可以检查用户状态以查看哪个文件已完成?

他是我正在尝试做的事情的简短代码片段。

// This function will be called for each file that is going to be downloaded.
// is there a way I can set the user state so I know that the download
// has been completed
// for that file, in the DownloadFileCompleted Event?
private void DownloadSingleFile()
{
if (!wb.IsBusy)
{
// Set user state here
wb.DownloadFileAsync(new Uri(downloadUrl), installationPath);
}
}


void wb_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
Console.WriteLine("File userstate: [ " + e.UserState + " ]");
}

void wb_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine("File userstate: [ " + e.UserState + " ]");

double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;

progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());

}

最佳答案

您可以将任何对象作为第三个​​ 参数传递给 DownloadFileAsync() 调用,您将把它作为 userState 取回。在你的情况下,你可以简单地传递你的文件名。

关于c# - 异步下载时设置用户状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1216252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com