C# 一个progressbar与多个WebClient

C# 一个progressbar与多个WebClient,c#,progress-bar,webclient,C#,Progress Bar,Webclient,问题是如何拥有一个progressbar和多个WebClient?这个场景不起作用,因为每个客户端都在自己更新bar,而且它正在变得疯狂,那么正确的方法是什么呢?另外,我不能只使用一个网络客户端,我以前对每个文件都有请求。我想你可以这样做: foreach (string line in textBox3.Lines) { int pos = line.IndexOf(

问题是如何拥有一个progressbar和多个WebClient?这个场景不起作用,因为每个客户端都在自己更新bar,而且它正在变得疯狂,那么正确的方法是什么呢?另外,我不能只使用一个网络客户端,我以前对每个文件都有请求。

我想你可以这样做:

                    foreach (string line in textBox3.Lines)
                    {
                        int pos = line.IndexOf("?v=");
                        string videoid = line.Substring(pos + 3, 11);
                        GetFile(videoid);
                    }

        GetFile() {
        ...code

        WebClient webClient = new WebClient();
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
        webClient.DownloadFileAsync(new Uri(fileRequest), @textBox2.Text + @"\" + title + ".mp3");
    }

    private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }
公共类WebClientProgressManager:INotifyPropertyChanged
{
专用只读词典_clients=new Dictionary();
私有常量字符串TotalProgressPropertyName=“TotalProgress”;
公共无效添加(WebClient客户端)
{
if(客户端==null)
抛出新的ArgumentNullException(“客户端”);
如果(_clients.ContainsKey(client))返回;
client.DownloadProgressChanged+=(s,e)=>
{
如果(例如,百分比==100)
{
_客户端。删除((WebClient)s);
}
RaisePropertyChanged(TotalProgressPropertyName);
};
_客户端。添加(客户端,0);
}
私有void RaisePropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
调用(这是新的PropertyChangedEventArgs(propertyName));
}
}
公共发展
{
得到
{
if(_clients.Count==0)返回100;//这里需要一些东西来防止被零除
int progress=\u clients.Sum(client=>client.Value);
返回进度/_clients.Count;
}
}
#INotifyPropertyChanged的区域实现
公共事件属性更改事件处理程序属性更改;
#端区
}

我想你可以这样做:

                    foreach (string line in textBox3.Lines)
                    {
                        int pos = line.IndexOf("?v=");
                        string videoid = line.Substring(pos + 3, 11);
                        GetFile(videoid);
                    }

        GetFile() {
        ...code

        WebClient webClient = new WebClient();
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
        webClient.DownloadFileAsync(new Uri(fileRequest), @textBox2.Text + @"\" + title + ".mp3");
    }

    private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }
公共类WebClientProgressManager:INotifyPropertyChanged
{
专用只读词典_clients=new Dictionary();
私有常量字符串TotalProgressPropertyName=“TotalProgress”;
公共无效添加(WebClient客户端)
{
if(客户端==null)
抛出新的ArgumentNullException(“客户端”);
如果(_clients.ContainsKey(client))返回;
client.DownloadProgressChanged+=(s,e)=>
{
如果(例如,百分比==100)
{
_客户端。删除((WebClient)s);
}
RaisePropertyChanged(TotalProgressPropertyName);
};
_客户端。添加(客户端,0);
}
私有void RaisePropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
调用(这是新的PropertyChangedEventArgs(propertyName));
}
}
公共发展
{
得到
{
if(_clients.Count==0)返回100;//这里需要一些东西来防止被零除
int progress=\u clients.Sum(client=>client.Value);
返回进度/_clients.Count;
}
}
#INotifyPropertyChanged的区域实现
公共事件属性更改事件处理程序属性更改;
#端区
}

如果您有两个web客户端,一个完成了50%,另一个完成了40%,您希望它显示45%?e、 所有人的平均数?是的,差不多。但是每个客户都在发送自己的状态,他们的号码未知@mishe更新了我的答案,以确保getter不被零除。这可能很重要:如果您有两个web客户端,一个完成了50%,另一个完成了40%,您希望它显示45%?e、 所有人的平均数?是的,差不多。但是每个客户都在发送自己的状态,他们的号码未知@mishe更新了我的答案,以确保getter不被零除。这可能很重要:p