UploadProgressChanged with Foreach

时间:2013-03-28 21:31:08

标签: c# foreach webclient

好的,我有这个:

foreach (FileInfo fileinfo2 in Arquivos2)
{
    label8.Text = "Enviando(NFe): " + fileinfo2.Name + "...";
    label8.Update();
    WebClient client = new WebClient();
    client.Credentials = new System.Net.NetworkCredential(usuario, senha);
    client.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressCallback);
    client.UploadFile(new Uri("ftp://" + ftp + "/" + caminho + "//nf//" + fileinfo2.Name), "STOR", @"C:\\NFe\" + fileinfo2.Name);
    bar++;
    backgroundWorker1.ReportProgress(bar);
}

它的工作正常,但我有:

private void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{
    progressBar2.Value = e.ProgressPercentage;
    progressBar2.Update();
}

并且progressBar2没有任何反应......

如何使用uploadprogress更新progressBar2?我不能将UploadFile与UploadProgressChanged一起使用?

1 个答案:

答案 0 :(得分:2)

根据http://msdn.microsoft.com/en-us/library/system.net.webclient.uploadprogresschanged.aspx

  

每次异步上传进行时都会引发此事件。

它只由3个异步事件引发。您调用的版本是阻塞方法,在完成之前不会返回,并且不会引发进度事件。