C# 异步下载时设置用户状态
VS2008SP1 我正在使用web客户端异步下载一些文件 我有5个文件要下载 但是,我希望监视每个下载,并希望将用户状态设置为文件名,以便在ProgressCompletedEvent中检查用户状态以查看哪个文件已完成 他是我试图做的一个简短的代码片段C# 异步下载时设置用户状态,c#,webclient,C#,Webclient,VS2008SP1 我正在使用web客户端异步下载一些文件 我有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 s
// 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返回。在您的情况下,只需传递文件名即可 像这样的东西怎么样:
private void BeginDownload(
string uriString,
string localFile,
Action<string, DownloadProgressChangedEventArgs> onProgress,
Action<string, AsyncCompletedEventArgs> onComplete)
{
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged +=
(object sender, DownloadProgressChangedEventArgs e) =>
onProgress(localFile, e);
webClient.DownloadFileCompleted +=
(object sender, AsyncCompletedEventArgs e) =>
onComplete(localFile, e);
webClient.DownloadFileAsync(new Uri(uriString), localFile);
}
Action<string, DownloadProgressChangedEventArgs> onProgress =
(string localFile, DownloadProgressChangedEventArgs e) =>
{
Console.WriteLine("{0}: {1}/{2} bytes received ({3}%)",
localFile, e.BytesReceived,
e.TotalBytesToReceive, e.ProgressPercentage);
};
Action<string, AsyncCompletedEventArgs> onComplete =
(string localFile, AsyncCompletedEventArgs e) =>
{
Console.WriteLine("{0}: {1}", localFile,
e.Error != null ? e.Error.Message : "Completed");
};
downloader.BeginDownload(
@"http://url/to/file",
@"/local/path/to/file",
onProgress, onComplete);
调用两次,将得到如下输出
/路径/到/文件1:收到265/265字节(100%)/路径/到/文件1:已完成
/路径/到/文件2:收到2134/2134字节(100%)
/路径/to/file2:Completedclient.UploadFileCompleted+=新的UploadFileCompletedEventHandler(UploadFileCallback);UploadFileAsync(uri,“STOR”、源文件、文件名);私有void UploadFileCallback(对象发送方,UploadFileCompletedEventArgs e){MessageBox.Show(e.UserState.ToString());…}
private void BeginDownload(string uriString, string localFile)
{
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged +=
(object sender, DownloadProgressChangedEventArgs e) =>
Console.WriteLine("{0}: {1}/{2} bytes received ({3}%)",
localFile, e.BytesReceived,
e.TotalBytesToReceive, e.ProgressPercentage);
webClient.DownloadFileCompleted +=
(object sender, AsyncCompletedEventArgs e) =>
Console.WriteLine("{0}: {1}", localFile,
e.Error != null ? e.Error.Message : "Completed");
webClient.DownloadFileAsync(new Uri(uriString), localFile);
}