Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.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#_Progress Bar_Webclient Download - Fatal编程技术网

C#多个进度条用于多个文件下载

C#多个进度条用于多个文件下载,c#,progress-bar,webclient-download,C#,Progress Bar,Webclient Download,我正在写一个C#应用程序来上传和下载文件。下载使用WebClient对象及其DownloadAsycDownload方法。下载可以很好地处理多个文件。它可以下载我想要的任何文件 我的问题是,我无法在不同的进度条中显示所有文件的进度,这些进度条动态添加到表单的flowlayout控件中 这是我的代码: public ProgressBar[] bar; public int countBar=0; ... bar[countBar] = new ProgressBar(); f

我正在写一个C#应用程序来上传和下载文件。下载使用WebClient对象及其
DownloadAsycDownload
方法。下载可以很好地处理多个文件。它可以下载我想要的任何文件

我的问题是,我无法在不同的进度条中显示所有文件的进度,这些进度条动态添加到表单的
flowlayout控件中

这是我的代码:

public ProgressBar[] bar;
public int countBar=0;

...

    bar[countBar] = new ProgressBar();
    flowLayoutPanel1.Controls.Add(bar[countBar]);
    countBar++;

    request.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownoadInProgress);
    request.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
    request.DownloadFileAsync(new Uri(this.uri), localPath);

    byte[] fileData = request.DownloadData(this.uri);
    FileStream file = File.Create(localPath);
    file.Write(fileData, 0, fileData.Length);
    file.Close();
}

public void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    flowLayoutPanel1.Controls.Remove(bar[countBar]);
    countBar--;
    MessageBox.Show("Download Completed");
}

public void DownoadInProgress(object sender, DownloadProgressChangedEventArgs e)
{
    bar[countBar].Maximum = total_bytes;
    bar[countBar].Value = (int)e.BytesReceived;                
}

您正在使用计数来索引进度条,但一旦一个进度条完成,您将删除最后一个进度条,您确实应该删除与文件关联的进度条

在这种情况下,我建议使用
字典
(可能不是
WebCliet
——应该是事件中的
发送者类型)

然后,您可以完全删除
countBar
,并使用新方法:

public void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    // Can remove (WebClient) cast, if dictionary is <object, ProgressBar>
    var request = (WebClient)sender;
    flowLayoutPanel1.Controls.Remove(dic[request]);
    dic.Remove(request);
    MessageBox.Show("Download Completed");
}

public void DownoadInProgress(object sender, DownloadProgressChangedEventArgs e)
{
    var progBar = dic[(WebClient)sender];
    progBar.Value = e.ProgressPercentage;                
}
public void DownloadFileCompleted(对象发送方,AsyncCompletedEventArgs e)
{
//可以删除(WebClient)强制转换,如果字典是
var请求=(网络客户端)发送方;
flowLayoutPanel1.控件。移除(dic[请求]);
删除(请求);
MessageBox.Show(“下载完成”);
}
public void downloadinprogress(对象发送方,DownloadProgressChangedEventArgs e)
{
var progBar=dic[(网络客户端)发送方];
progBar.值=e.ProgressPercentage;
}

我会在lambdas中使用类似的内容,更简洁一些,不需要在字典中使用:

var webClient = new WebClient();

var pb = new ProgressBar();
pb.Maximum = 100;
flowLayoutPanel1.Controls.Add(pb);

webClient.DownloadProgressChanged += (o, args) =>
{
    pb.Value = args.ProgressPercentage;
};

webClient.DownloadFileCompleted += (o, args) =>
{
    flowLayoutPanel1.Controls.Remove(pb);
};

webClient.DownloadFileAsync(new Uri(this.uri), localPath);

谢谢,这真的很有帮助。如果这对你完全有帮助的话,把它标记为可接受的答案。否则,请告诉我缺少什么。我正在尝试添加来自其他类的组件。无法在运行时将进度条添加到表单中。我发布的这些方法在另一个类中。没有错误。但不会在运行时添加进度条。应在单击按钮时添加进度条。谢谢。我已经把它贴在这里了,如果你有什么想法的话
var webClient = new WebClient();

var pb = new ProgressBar();
pb.Maximum = 100;
flowLayoutPanel1.Controls.Add(pb);

webClient.DownloadProgressChanged += (o, args) =>
{
    pb.Value = args.ProgressPercentage;
};

webClient.DownloadFileCompleted += (o, args) =>
{
    flowLayoutPanel1.Controls.Remove(pb);
};

webClient.DownloadFileAsync(new Uri(this.uri), localPath);