异步下载时设置用户状态

ant*_*009 5 c# webclient

VS 2008 SP1

我正在使用web clicent异步下载一些文件.

我有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());

}
Run Code Online (Sandbox Code Playgroud)

Pau*_*Jan 11

您可以将任何对象作为第三个参数传递给DownloadFileAsync()调用,然后您将其作为userState返回.在您的情况下,您可以简单地传递您的文件名.